【发布时间】:2017-10-17 13:58:07
【问题描述】:
我在 Ruby 中有一个哈希值,我正在向其中插入新的哈希值或向现有哈希值添加值。 我一直觉得 Ruby 有更好的方法来做到这一点:
map # => { 1 => {:type => "humbug", :name => "grinch" }, 2 => {:type => 2 } }
if map[key]
map[key].store(:name, value)
else
map[key] = { name: value }
end
我希望能够做类似的事情
map[key].store(:name, value) || map[key] = {name: value}
但如果map[key] 处没有value,当然会失败
...建议?
【问题讨论】:
-
Ruby 中没有“地图”,有哈希。
-
@meagar 破产了!我整天都在使用 groovy ......其中到处都有地图:D
标签: ruby-on-rails ruby hashmap