尝试向数组添加任何内容时,不会将nil隐式转换为整数 [英] no implicit conversion from nil to integer - when trying to add anything to array
问题描述
没有从nil到隐式转换的结果我试图构建一个相当复杂的散列,我很奇怪地得到错误整数
当我使用该行时
manufacturer_cols<< {:field => 'test'}
稍后在同一个循环中使用同一行,并且它没有任何问题。
整个代码是
$ p $ manufacturer_cols = []
manufacturer_fields.each_with_index do | mapped_field,index |
如果mapped_field.base_field_name =='exactSKU'
#这是它破坏的地方,如果我评论这一点,一切都很好
manufacturer_cols<< {:base_field => 'test'}
else
#在这里可以正常工作!
manufacturer_cols<< {:base_field => mapped_field.base_field_name}
结束
结束
- ------ manufacturer_fields的值--------
[{base_field:{base_field_name:Category,id :1, 名称: 类别}},{ base_field:{ base_field_name: 描述, ID:3 名称: SHORT_DESCRIPTION}},{ base_field:{ base_field_name : exactSKU, ID:5 名称: Item_SKU}},{ base_field:{ base_field_name: 标记, ID:25, 姓名: Retail_Price }},{base_field:{base_field_name:Family,id:26,name:Theme}}]
隐式转换错误解释
我不确定您的代码为什么会出现此错误,但我可以准确地告诉您错误的含义,也许这将有所帮助。
Ruby中有两种转换类型: explicit 和隐式。 / p> 显式转换使用短名称,如 隐式转换使用长名称
#to_s
或 #to_i。
这些a通常在核心中定义,并且它们始终被调用。它们用于不是字符串或不是整数的对象,但可以转换为调试或数据库转换或字符串插值或任何其他类型。
#to_str
或 #to_int。
这种转换适用于非常像字符串或整数的对象,只需要知道什么时候采取他们的自我形式。这些转换从未或几乎从未在核心中定义。 (Hal Fulton的 Way #to_str
的原因的类之一。
很难得到你的错误,甚至 NilClass
定义了显式(短名称)转换器:
nil.to_i
=> 0
>>#{nil}<< #这演示了nil.to_s
=> >><<
您可以像这样触发它:
Array.new nil
TypeError:不存在从nil到整数的隐式转换
因此,您的错误来自Ruby解释器中的C代码。在C语言中实现的核心类当它需要一个整数
时,会被交给一个 nil
。它可能有一个 #to_i
但它没有 #to_int
,所以结果是 TypeError。
I'm trying to build a fairly complex hash and I am strangely getting the error
no implicit conversion from nil to integer
when I use the line
manufacturer_cols << {:field => 'test'}
I use the same line later in the same loop, and it works no problem.
The entire code is
manufacturer_cols=[]
manufacturer_fields.each_with_index do |mapped_field, index|
if mapped_field.base_field_name=='exactSKU'
#this is where it is breaking, if I comment this out, all is good
manufacturer_cols << { :base_field=> 'test'}
else
#it works fine here!
manufacturer_cols << { :base_field=>mapped_field.base_field_name }
end
end
------- value of manufacturer_fields --------
[{"base_field":{"base_field_name":"Category","id":1,"name":"Category"}},{"base_field":{"base_field_name":"Description","id":3,"name":"Short_Description"}},{"base_field":{"base_field_name":"exactSKU","id":5,"name":"Item_SKU"}},{"base_field":{"base_field_name":"Markup","id":25,"name":"Retail_Price"}},{"base_field":{"base_field_name":"Family","id":26,"name":"Theme"}}]
Implicit Conversion Errors Explained
I'm not sure precisely why your code is getting this error but I can tell you exactly what the error means, and perhaps that will help.
There are two kinds of conversions in Ruby: explicit and implicit.
Explicit conversions use the short name, like #to_s
or #to_i.
These are commonly defined in the core, and they are called all the time. They are for objects that are not strings or not integers, but can be converted for debugging or database translation or string interpolation or whatever.
Implicit conversions use the long name, like #to_str
or #to_int.
This kind of conversion is for objects that are very much like strings or integers and merely need to know when to assume the form of their alter egos. These conversions are never or almost never defined in the core. (Hal Fulton's The Ruby Way identifies Pathname as one of the classes that finds a reason to define #to_str
.)
It's quite difficult to get your error, even NilClass
defines explicit (short name) converters:
nil.to_i
=> 0
">>#{nil}<<" # this demonstrates nil.to_s
=> ">><<"
You can trigger it like so:
Array.new nil
TypeError: no implicit conversion from nil to integer
Therefore, your error is coming from the C code inside the Ruby interpreter. A core class, implemented in C, is being handed a nil
when it expects an Integer
. It may have a #to_i
but it doesn't have a #to_int
and so the result is the TypeError.
这篇关于尝试向数组添加任何内容时,不会将nil隐式转换为整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!