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

前端 未结 4 786
天涯浪人
天涯浪人 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
    

提交回复
热议问题