我想从内部哈希中获取值。在这种情况下,RU-始终是第一个值。
PAYMENT_TYPE_TO_CURRENCY_AND_COUNTRY_MAPPING = {
zimpler: { 'EUR' => ['FI'], 'SEK' => ['SE'] },
qiwi: { 'EUR' => ['RU', 'KZ'], 'RUB' => ['RU'], 'KZT' => ['KZ'], 'USD' => ['UA'] },
payu: { 'CZK' => ['CZ'], 'PLN' => ['PL']},
entercash: { 'EUR' => ['AT', 'DE', 'FI'], 'SEK' => ['SE'] },
carulla: { 'USD' => ['CO'] }
}
我尝试过:
PAYMENT_TYPE_TO_CURRENCY_AND_COUNTRY_MAPPING.each do |payment_method_key, array|
p payment_method_key.to_s /// prints "qiwi" - OK
p array.keys.first /// prints "EUR" - OK
p array[array.keys.first] //// prints ["RU", "KZ"] - not OK - need only RU
end
如何实现此功能?
答案 0 :(得分:0)
对于我来说不清楚是您每次只想获取第一个元素(先前的答案完成了该操作),还是每次只想获取一个“特定”元素。允许您选择特定国家/地区而不管其在阵列中的何处的另一种方法是:
PAYMENT_TYPE_TO_CURRENCY_AND_COUNTRY_MAPPING.each_pair do |method, currencies|
currencies.each_pair do |currency, countries|
countries.each do |country|
p country if country == "RU"
end
end
end
output:
=> "RU"
"RU"
return value would be the original hash:
=> {:zimpler=>{"EUR"=>["FI"], "SEK"=>["SE"]},
:qiwi=>{"EUR"=>["RU", "KZ"], "RUB"=>["RU"], "KZT"=>["KZ"], "USD"=>["UA"]},
:payu=>{"CZK"=>["CZ"], "PLN"=>["PL"]},
:entercash=>{"EUR"=>["AT", "DE", "FI"], "SEK"=>["SE"]},
:carulla=>{"USD"=>["CO"]}}