【发布时间】:2017-01-06 20:44:35
【问题描述】:
我正在尝试循环一个哈希数组。当我到达获取枚举器开始循环的点时,我收到以下错误:
undefined method `[]' for nil:NilClass
我的代码如下所示:
def extraireAttributs (attributsParam)
classeTrouvee = false
scanTrouve = false
ownerOSTrouve = false
ownerAppTrouve = false
resultat = Hash.new(0)
attributs = Array(attributsParam)
attributs.each do |attribut| #CRASHES HERE!!!
typeAttribut = attribut['objectTypeAttribute']
[...]
我在调试模式下检查以确保 attributsParamsargument 和 attributsvariable 不是 nil 或空的。两者(因为它们是相同的!)都包含 59 个哈希对象,但我仍然无法在数组上获得枚举器。
为什么我不断收到这个错误?
谢谢!
【问题讨论】:
-
你确定它在那里而不是下一行?
-
你确定它不在后面的代码上吗?没有行号很难知道,能否提供行号?
-
attributsParam长什么样子?如果哈希值太多,只显示其中的 3 个!
标签: arrays ruby hash enumerator