我有这个密码:

price = variant[merchant["variant_code"]]

也就是说:variant["PRDCT-A"]
但我的问题是它返回nil,因为我的variant散列看起来是这样的:{:"PRDCT-A"=>{:price=>1495.0}}它有:但我只是传递"PRDCT-A"应该是:"PRDCT-A"
我尝试过concat,但它不起作用,我如何包含冒号以便访问散列值?

最佳答案

如果我没有错的话,你的哈希结构可能是

merchant = {variant_code: 'PRDCT-A'}
variant = {:"PRDCT-A"=>{:price=>1495.0}}

你想借助商家散列访问变量值
你应该试试这个,
variant.dig(merchant[:variant_code].to_sym)

关于ruby-on-rails - 无法访问哈希值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57703949/

10-14 16:03