0

有人知道为什么

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

得到以下错误:

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

谢谢

4

1 回答 1

2

如果您想拥有“多维”哈希,您需要正确定义哈希,如下所示:

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

然后你可以这样做:

a['city']['answer0'] = 'foo'
于 2013-03-11T22:01:08.720 回答