Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 未定义的方法“@';在轨_Ruby On Rails_Ruby_Ruby On Rails 3.2 - Fatal编程技术网

Ruby on rails 未定义的方法“@';在轨

Ruby on rails 未定义的方法“@';在轨,ruby-on-rails,ruby,ruby-on-rails-3.2,Ruby On Rails,Ruby,Ruby On Rails 3.2,有人知道为什么吗 hash['City'] = {} hash['City']['answer0'] = 'foo' 获取以下错误: undefined method `+@' for {"answer0"=>"foo"}:Hash 谢谢如果您想使用“多维”散列,您需要正确定义散列,如下所示: a = Hash.new { |hash, key| hash[key] = Hash.new(&hash.default_proc) } 然后你可以做: a['city']['ans

有人知道为什么吗

hash['City'] = {}
hash['City']['answer0'] = 'foo'
获取以下错误:

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

谢谢

如果您想使用“多维”散列,您需要正确定义散列,如下所示:

a = Hash.new { |hash, key| hash[key] = Hash.new(&hash.default_proc) }
然后你可以做:

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

当您首先初始化散列时会发生什么,如
散列={}
?您没有显示得到错误的那一行。您可以添加一个堆栈跟踪来记录此错误发生的位置吗?它不是发生在这里的作业,而是发生在其他地方。你发布的代码看起来不错。发布更多代码?我想你把哈希初始化搞砸了