从地图获取值

时间:2018-09-14 08:42:02

标签: ruby hash

我有这个哈希值:

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']},
  davivienda: {'USD' => ['CO']},
  banco_de_chile: {'USD' => ['CL']}
}

我要使用sample从结构中随机选择货币和国家。我做到了:

currency = PAYMENT_TYPE_TO_CURRENCY_TO_COUNTRY_BY_PAYMENT_TYPE_MAPPING[payment_type].keys.sample
country = PAYMENT_TYPE_TO_CURRENCY_TO_COUNTRY_BY_PAYMENT_TYPE_MAPPING[payment_type][currency].sample

我得到:

undefined method `keys' for nil:NilClass (RuntimeError)

我该如何实现?

0 个答案:

没有答案