Reputation:
require_relative 'json_lookup'
require_relative 'csv_lookup'
require_relative 'error'
BASE_RATE = 'EUR'
class CurrencyExchange
def initialize(file:, date:, from:, to:)
@file = file
@date = date
@from = from
@to = to
end
def rate
lookup = find_lookup
lookup.to_currency / lookup.from_currency
end
private
def find_lookup
case File.extname(@file)
when ".json"
JsonLookup.new(@file, @date, @from, @to)
when ".csv"
CsvLookup.new(@file, @date, @from, @to)
else raise FileError
end
end
end
I keep on getting this error for when i run the CurrencyExchange.rate in irb so I am guessing something is going wrong with the rate method but can't figure it out as to why. But I may be missing something completely obvious... As I am a complete beginner at Ruby, would appreciate any help :)
The traceback is as follows..
irb(main):003:0> CurrencyExchange.rate(Date.new(2018, 11, 22), "USD", "GBP") Traceback (most recent call last):
5: from C:/Ruby26-x64/bin/irb.cmd:31:in `<main>'
4: from C:/Ruby26-x64/bin/irb.cmd:31:in `load'
3: from C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/irb-1.0.0/exe/irb:11:in `<top (required)>'
2: from (irb):3
1: from (irb):3:in `rescue in irb_binding'
NoMethodError (undefined method `rate' for CurrencyExchange:Class)
Upvotes: 2
Views: 2858
Reputation: 106782
rate
is an instance method in your example, but CurrencyExchange.rate
tries to call a class method.
To solve this, initialize an instance first and call then rate
on that instance. Furthermore, rate
doesn't accept arguments, you need to pass the variables to the initializing method.
currency_exchange = CurrencyExchange.new(
file: file, date: Date.new(2018, 11, 22), from: "USD", to: "GBP"
)
currency_exchange.rate
Please note that the initializer expects 4 named arguments. You will need to pass a file to the new
method too.
Upvotes: 2