有人知道原因吗
hash['City'] = {}
hash['City']['answer0'] = 'foo'
收到以下错误:
undefined method `+@' for {"answer0"=>"foo"}:Hash
由于
答案 0 :(得分:2)
如果你想要'多维'哈希,你需要像这样正确定义哈希:
a = Hash.new { |hash, key| hash[key] = Hash.new(&hash.default_proc) }
然后你可以这样做:
a['city']['answer0'] = 'foo'