在Ruby哈希中寻找价值

时间:2018-10-02 12:33:10

标签: ruby

我有这个用于获取金额限制的Ruby哈希:

AMOUNT_LIMITS_FOR_TRX_TYPE = {
    zimpler: {
      eur: { min: 3_50,     max: 15_00_00 },
      sek: { min: 35_00,    max: 15_000_00 }
    },
    qiwi: {
      eur: { min: 5_00,     max: 350_00 },
      usd: { min: 5_00,     max: 500_00 },
      rub: { min: 500_00,   max: 15_000_00 },
      kzt: { min: 500_00,   max: 74_300_00 }
    },
    payu: {
      czk: { min: 1,        max: 1_000_000_00 },
      pln: { min: 1,        max: 1_000_000_00 }
    }
}.freeze

def amount_for(transaction_type, currency)
    p transaction_type
    p currency
    min_amount = AMOUNT_LIMITS_FOR_TRX_TYPE[transaction_type.to_sym][currency.to_sym][:min]
    max_amount = AMOUNT_LIMITS_FOR_TRX_TYPE[transaction_type.to_sym][currency.to_sym][:max]
    rand(min_amount..max_amount)
  end

我使用参数:zimpler"SEK"来对方法amount_for进行校准,但出现错误:

checks.rb:31:in `execute': undefined method `[]' for nil:NilClass (RuntimeError)
/models/request_builders/request_builder.rb:246:in `amount_for'

您知道我的错误在哪里以及如何解决吗?

1 个答案:

答案 0 :(得分:5)

您要传递"SEK"作为currency参数,然后尝试通过以下方式访问哈希值:

currency.to_sym

但是它返回:SEK而不是:sek。您可能需要将其修复为:

currency.downcase.to_sym