简体   繁体   中英

No implicit conversion of string into integer on ruby on rails

I am trying to identify this problem: "no implicit conversion of String into Integer"

The problem is in this line <% if coin == x["symbol"] %>

The code:

<% for x in @coins %>
    <% for coin in @my_coins %>
        <% if coin == x["symbol"]%>
            <%= x["name"]%>
        <%end%>
    <%end%>
<%end%>

The variables

class HomeController < ApplicationController
  def index
    require 'net/http'
    require 'json'
    @url = 'https://pro-api.coinmarketcap.com/v1/cryptocurrency/listings/latest?start=1&limit=100&CMC_PRO_API_KEY=MyKey'
    @uri = URI(@url)
    @response = Net::HTTP.get(@uri)
    @coins = JSON.parse(@response)
    @my_coins = ["BTC", "XRP", "ADA", "ETH", "USDT"]
  end
  def about
  end 
  def lookup
  end
end

The first lines of API and the first example:


"data":
[
{
"id":1,"name":"Bitcoin",
"symbol":"BTC",
"slug":"bitcoin",
"num_market_pairs":9676,
"date_added":"2013-04-28T00:00:00.000Z",
"tags":["mineable","pow","sha-256","store-of-value","state-channels","coinbase-ventures-portfolio","three-arrows-capital-portfolio","polychain-capital-portfolio"],
"max_supply":21000000,
"circulating_supply":18629531,
"total_supply":18629531,"platform":null,"cmc_rank":1,"last_updated":"2021-02-15T15:54:02.000Z",
"quote":{"USD":{"price":47907.843633498785,
"volume_24h":79376349219.00912,
"percent_change_1h":-0.13222586,
"percent_change_24h":-1.5128616,
"percent_change_7d":10.59948497,
"percent_change_30d":28.8994407,
"market_cap":892500658113.4182,
"last_updated":"2021-02-15T15:54:02.000Z"}}},


The root of the problem is that x isn't a hash. I expect it's an array.

For example this will give you the same error:

[]["symbol"]

Basically, when x is an array, the input of the [] method expects an integer, and therefore tries to convert what is passed in, into an integer. The error occurs when the conversion fails.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM