在Ruby语言中,从下面的嵌套散列中通过language检索正确的散列的最快方法是什么?
COUNTRIES = {
"DE" => {:currency => "EUR", :tax_rates => [19, 7], :languages => ["de-de"]},
"US" => {:currency => "USD", :tax_rates => [10], :languages => ["en-us"]}
}通过提供currency,我设法检索到了正确的哈希值
COUNTRIES.select{|key, hash| hash[:currency] == "USD" }但是,如何通过language进行查询,例如通过en-us
谢谢你的帮助。
发布于 2018-01-19 18:36:12
COUNTRIES.select{|key, hash| hash[:languages].include?("en-us")}
值得注意的是,如果您只想得到一个单一的结果,那么使用#detect比使用#select.first更便宜
https://stackoverflow.com/questions/48339072
复制相似问题