假设我有这样的哈希:
h = {100 => ["a","b"], 200 => ["c","d"]}
当我知道其中一个数组值(如a或d)时,获取相应键(如100或200)的最快/最简洁的方法是什么?我可以使用find
或select
或map
吗?
如果我的哈希是这样的话会更容易吗?
h = {["a","b"] => 100, ["c","d"] => 200}
答案 0 :(得分:2)
哈希使用{}
代替[]
。但你可以这样做:
h = {100 => ["a","b"], 200 => ["c","d"]}
desired_value = "a" # the value you want to search for
h.select { |k,v| v.include? desired_value }.keys