Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby:替换多维数组中的匹配元素?_Ruby_Arrays - Fatal编程技术网

Ruby:替换多维数组中的匹配元素?

Ruby:替换多维数组中的匹配元素?,ruby,arrays,Ruby,Arrays,有人能告诉我如何替换这个2D数组中的元素吗?我尝试了每种方法,包括和替换,但都无法找出哪里出了问题。提前感谢您的帮助 class Lotto def initialize @lotto_slip = Array.new(5) {Array(6.times.map{rand(1..60)})} end def current_pick @number = rand(1..60).to_s puts "The number is #{@number}."

有人能告诉我如何替换这个2D数组中的元素吗?我尝试了每种方法,包括和替换,但都无法找出哪里出了问题。提前感谢您的帮助

class Lotto

  def initialize
    @lotto_slip = Array.new(5) {Array(6.times.map{rand(1..60)})}
  end

  def current_pick
    @number = rand(1..60).to_s
    puts "The number is #{@number}."
  end

  def has_number
    #prints out initial slip
      @lotto_slip.each {|x| p x}

    #Prints slip with an "X" replacing number if  is on slip
    #Ex: @number equals 4th number on slip  --> 1, 2, 3, X, 5, 6
      @lotto_slip.each do |z|
      if z.include?(@number)
          z = "X"
          p @lotto_slip
      else
          z = z
          p @lotto_slip
      end
    end  
  end
end



test = Lotto.new
test.current_pick
test.has_number

让我知道这是否可行(尝试将变化从1减少到10,以便能够更轻松地进行测试):

我在代码中看到的问题是:

  • 对于这行
    @number=rand(1..60),您不需要
    到_s
    。到_s
    ,否则您将如何将数组生成的数字与实际字符串进行比较

  • 您需要重新生成数组,而不是重新分配,这就是为什么我将所有代码替换为
    z.map!{| x | x=@number?'x':x}
    基本上重新生成整个数组


    • 无需对每个
      进行迭代,使用
      map

      @lotto_slip = Array.new(5) {Array(6.times.map{rand(1..60)})}
      #=> [[25, 22, 10, 10, 57, 17], [37, 4, 8, 52, 55, 7], [44, 30, 58, 58, 50, 19], [49, 49, 24, 31, 26, 28], [24, 18, 39, 27, 8, 54]]
      
      @number = 24
      @lotto_slip.map{|x| x.map{|x| x == @number ? 'X' : x}}
      #=> [[25, 22, 10, 10, 57, 17], [37, 4, 8, 52, 55, 7], [44, 30, 58, 58, 50, 19], [49, 49, "X", 31, 26, 28], ["X", 18, 39, 27, 8, 54]]
      

      太棒了,谢谢。我正在学习语法和正确的使用方法。我真的很感谢你的帮助,看起来。map将是下一个学习的对象。旁白:你可以写
      @lotto_slip=Array.new(5){Array.new(6){rand(1..60)}
      @lotto_slip = Array.new(5) {Array(6.times.map{rand(1..60)})}
      #=> [[25, 22, 10, 10, 57, 17], [37, 4, 8, 52, 55, 7], [44, 30, 58, 58, 50, 19], [49, 49, 24, 31, 26, 28], [24, 18, 39, 27, 8, 54]]
      
      @number = 24
      @lotto_slip.map{|x| x.map{|x| x == @number ? 'X' : x}}
      #=> [[25, 22, 10, 10, 57, 17], [37, 4, 8, 52, 55, 7], [44, 30, 58, 58, 50, 19], [49, 49, "X", 31, 26, 28], ["X", 18, 39, 27, 8, 54]]