我有哈希是红宝石:在哈希匹配关键字返回各自的键+值
hash = {"stars"=>"in the galaxy", "fin"=>"is for fish", "fish"=>"has fins"}
现在我有一个find方法
def find(value)
if hash.empty? == true
return {}
else
return hash
end
end
现在运行时,我想要做的是 - find("fi")
,我想要该方法返回密钥中包含fi
的所有散列键+值。因此,看起来像这样的输出 -
{"fin"=>"is for fish", "fish"=>"has fins"}
请注意“fi”不是固定的。它可以是任何东西。由于Find方法接受参数value
。
任何帮助或建议表示感谢。我尝试了hash#select。但没有如此有用。我不确定在这里使用什么。
寮步..我不知道它看起来的所有匹配的情况。非常感谢 :)。 – 2013-02-11 14:51:03