替换嵌套数组 ruby​​ 中的元素 [英] replacing an element in nested array ruby

查看:39
本文介绍了替换嵌套数组 ruby​​ 中的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm having trouble locating where my issue is in my code. I want to replace specific elements with 'X' if they show up on a bingo board:

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 从未出现,我现在已经多次查看我的代码,但不知道为什么...:

this is the test i'm using to see if my code is running, but X never shows up and I been looking over my code many times now and can't figure out why...:

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

如果有人能提供有关我遗漏或未看到的见解,我将不胜感激!

If anyone can provide insight on what I am missing or not seeing, I would greatly appreciate it!

推荐答案

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

这篇关于替换嵌套数组 ruby​​ 中的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆