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

前端 未结 4 767
天涯浪人
天涯浪人 2021-01-04 03:14

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 l

相关标签:
4条回答
  • 2021-01-04 03:50

    I got this error when parsing through an API for "tag/#{idnum}/parents"...Normally, you'd expect a response like this:

     {
    
    "parents": [
        {
            "id": 8,
            "tag_type": "MarketTag",
            "name": "internet",
            "display_name": "Internet",
            "angellist_url": "https://angel.co/internet",
            "statistics": {
                "all": {
                    "investor_followers": 1400,
                    "followers": 5078,
                    "startups": 13214
                },
                "direct": {
                    "investor_followers": 532,
                    "followers": 1832,
                    "startups": 495
                }
            }
        }
    ],
    "total": 1,
    "per_page": 50,
    "page": 1,
    "last_page": 1
    

    }

    but when I looked up the parents of the market category "adult" (as it were), I got this

    {
    
    "parents": [ ],
    "total": 0,
    "per_page": 50,
    "page": 1,
    "last_page": 0
    
    }
    

    Now ruby allowed a number of interactions with this thing to occur, but eventually it threw the error about implicit conversion

        parents.each do |p|
            stats = p['statistics']['all']
            selector << stats['investor_followers'].to_i
        end
        selected = selector.index(selector.max)
        parents[selected]['id'] ***<--- CODE FAILED HERE
    end
    
    0 讨论(0)
  • 2021-01-04 03:52

    This seems to have been completely unrelated to anything that had anything to do with manufacturer_cols after all.

    I had arrived at the manufacturer_cols bit because if I commented that out, it ran fine.

    However, if I commented out the part where I ran through the csv further down the page, it ran fine also.

    It turns out the error was related to retrieving attempting to append the base_field when it was nil.

    I thought I could use

    manufacturer_cols.each do |col|
       base_value = row[col[:row_index].to_i]
    
       if col[:merges]
           col[:merges].each do |merge|
               base_value += merge[:separator].to_s + row[merge[:merge_row_index]]
           end
       end
    end
    

    unfortunately, that caused the error. the solution was

     base_value = base_value + merge[:separator].to_s + row[merge[:merge_row_index]]
    

    I hope this helps somebody, 'cause as DigitalRoss alluded to, it was quite a wild goose chase nailing down where in the code this was being caused and why.

    0 讨论(0)
  • 2021-01-04 04:11

    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.

    0 讨论(0)
  • 2021-01-04 04:13

    This was a simple fix for me.

    When I was getting this error using the Scout app, one of my mapped folders was header-1, when I removed the hyphen from the folder name and made it header1, the error went away.

    It didn't like the hyphen for some reason...

    0 讨论(0)
提交回复
热议问题