M
M
Master Ruby2020-03-02 21:01:44
ruby
Master Ruby, 2020-03-02 21:01:44

Am I designing the class and subclass correctly?

Good time guys!
Gradually mastering Ruby. Decided to design a Ruby class to apply new knowledge and further develop an online currency converter application . And of course, I would like to hear the advice of senior comrades, did I start correctly:

# frozen_string_literal: true

require 'json'
require 'net/http'

# superclass
class Currency
  def bank
    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)
    result
  end
end

# subclass
class Usd < Currency
  def exchange_rub(dollar)
    money = bank
    rub = money[:rates][:RUB]
    puts rub * dollar
  end

  def exchange_eur(dollar)
    money = bank
    eur = money[:rates][:EUR]
    puts dollar * eur
  end
end

usd = Usd.new
usd.exchange_rub(100)
usd.exchange_eur(100)

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question