替换嵌套数组ruby中的元素

时间:2015-04-13 18:15:40

标签: ruby nested

我无法找到代码中的问题所在。如果特定元素出现在宾果板上,我想用'X'替换它们:

class BingoBoard

  def initialize(board)
    @bingo_board = board
  end

  def number_letter

    @letter = ['B','I','N','G','O'].sample
    @number = rand(1..100)

  end

  def checker
    @number
    @bingo_board.map! do |n|

      if n.include?(@number)

        n.map! { |x| x == @number ? 'X' : x}

      else

        n

      end
    end

  end

end

这是我正在使用的测试,看看我的代码是否正在运行,但X从未出现过,我现在多次查看我的代码并且无法找出原因......:

board = [[47, 44, 71, 8, 88],
        [22, 69, 75, 65, 73],
        [83, 85, 97, 89, 57],
        [25, 31, 96, 68, 51],
        [75, 70, 54, 80, 83]]

new_game = BingoBoard.new(board)

new_game.checker

如果有人能够提供我所缺少或未看到的内容,我将不胜感激!

1 个答案:

答案 0 :(得分:0)

class BingoBoard

  def initialize(board)
    @bingo_board = board
  end

  def number_letter

    @letter = ['B','I','N','G','O'].sample
    @number = rand(1..100)

  end

  def checker
    number_letter
    @bingo_board.each do |n|
      index = n.index(@number)
      n[index] = 'X' unless index.nil?
    end

  end

end