如何从二维数组中检索字符串

时间:2013-07-12 04:55:20

标签: html ruby-on-rails ruby

def foo(val)
{
  one: [["one1", "1"]],
  two: [
         ["two1", "1"],
         ["two2", "2"]],
}[val]
end

我想从“1”中检索“two1”,我该怎么做?

注意:我想做[:two] [“1”]

1 个答案:

答案 0 :(得分:2)

def foo(v1, v2)
  hsh = {
    'one' => [["one1", "1"]],
    'two' => [ ["two1", "1"],
         ["two2", "2"] ],
  }
  hsh[v1].select{|i| i[0] == (v1 + v2) && v2 == i[1] }.first.first
end