rails中未定义的方法`@'

时间:2013-03-11 17:13:34

标签: ruby-on-rails ruby ruby-on-rails-3.2

有人知道原因吗

hash['City'] = {}
hash['City']['answer0'] = 'foo'

收到以下错误:

undefined method `+@' for {"answer0"=>"foo"}:Hash

由于

1 个答案:

答案 0 :(得分:2)

如果你想要'多维'哈希,你需要像这样正确定义哈希:

a = Hash.new { |hash, key| hash[key] = Hash.new(&hash.default_proc) }

然后你可以这样做:

a['city']['answer0'] = 'foo'