Ruby嵌套数组

时间:2012-03-16 22:40:56

标签: ruby arrays multidimensional-array

我有一个嵌套数组,如下所示:

@nested = [
  ['1','2','3'],
  ['1','5','9'],
  ['1','4','7'],
  ['3','5','7'],
  ['3','6','9'],
  ['7','8','9'],
  ['4','5','6'],
  ['2','5','8']
]

我想获取任何整数(1..9)的用户输入,并查找具有该输入整数的每个数组。

不知道怎么做。

1 个答案:

答案 0 :(得分:8)

使用select

num_to_search = "9"
@nested.select do |array|
  array.include? num_to_search
end
#=> [["1", "5", "9"], ["3", "6", "9"], ["7", "8", "9"]]