Ruby 将多维数组中的字符串强制转换为整数

Ruby 将多维数组中的字符串强制转换为整数,ruby,casting,integer,typeerror,Ruby,Casting,Integer,Typeerror,我有一个变量(result),它在执行YAML::dump(result)时如下所示: 我想对enid进行条件比较,如下所示: if result["response"]["docs"]["enid"].to_i == num['1']['car'] 其中num['1']['car']是一个整数 每当我尝试这样做时,就会抛出一个TypeError can't convert String into Integer (TypeError) 即使我尝试 result["response"]["d

我有一个变量(
result
),它在执行
YAML::dump(result)
时如下所示:

我想对
enid
进行条件比较,如下所示:

if result["response"]["docs"]["enid"].to_i == num['1']['car']
其中
num['1']['car']
是一个整数

每当我尝试这样做时,就会抛出一个TypeError

can't convert String into Integer
(TypeError)
即使我尝试

result["response"]["docs"]["enid"].to_i


如何将我的
enid
值转换为整数,以便进行比较?

问题在于
结果[“response”][“docs”]
中的内容不是散列,而是像散列一样寻址的。在这种情况下,您需要的是
result[“response”][“docs”][0][“enid”]
。如果您想了解原因,请尝试
p result[“response”]
查看在每个级别使用的是什么Ruby数据结构。YAML在这里可能有点误导,即使您已经阅读了一段时间。

太好了!谢谢你,彼得!恰到好处。尝试使用[0]术语,效果良好。希望这对其他人也有帮助!
result["response"]["docs"]["enid"].to_i
Integer(result["response"]["docs"]["enid"])