rubyhash

no implicit conversion from nil to integer - when trying to add anything to array


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"}}]


Solution

  • 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.