M
M
Magic Code2020-03-02 16:58:04
ruby
Magic Code, 2020-03-02 16:58:04

Why does it return nil?

Good afternoon!
I practice OOP. Here, I decided to get the value of the key, but it gives me nil:

# frozen_string_literal: true

require 'json'
require 'net/http'

class Forex
  @@response = Net::HTTP.get_response(URI.parse('https://openexchangerates.org/api/latest.json?app_id=74d160e9151c48fbafa8c5487546886d'))
  @@response_body = @@response.body
  @@result = JSON.parse(@@response_body, :symbolize_names => true)
  
  attr_reader :cash, :face_value 

  def cash=(value)
    @cash = value
  end


  def read
    p @@result[:rates][:@cash]
  end
end

money = Forex.new
money.cash = 'RUB'
money.read

If you put RUB instead of [:cash], then everything works! How can I fix it? Can I use variables to get the result? Thanks in advance!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
oh_shi, 2020-03-02
@Panda_Code

p @@result[:rates][@cash.to_sym]

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question