class MagicMoney
Constants
- RATES
Attributes
amount[RW]
currency[RW]
Public Class Methods
new(amount, currency)
click to toggle source
# File lib/magic_money.rb, line 12 def initialize(amount, currency) validate_currency(currency) @amount = amount @currency = currency end
Public Instance Methods
convert_to(new_currency)
click to toggle source
# File lib/magic_money.rb, line 22 def convert_to(new_currency) validate_currency(new_currency) MagicMoney.new(amount_in(new_currency), new_currency) end
inspect()
click to toggle source
# File lib/magic_money.rb, line 18 def inspect "#{ amount_as_money } #{ @currency }" end
Private Instance Methods
amount_as_money()
click to toggle source
# File lib/magic_money.rb, line 28 def amount_as_money '%.2f' % @amount end
amount_in(new_currency)
click to toggle source
# File lib/magic_money.rb, line 32 def amount_in(new_currency) new_currency == @currency ? @amount : (@amount.to_f * RATES[@currency][new_currency]).round(2) end
validate_currency(currency)
click to toggle source
# File lib/magic_money.rb, line 36 def validate_currency(currency) raise ArgumentError, 'Invalid currency' if RATES[currency].nil? end