class MoneyDt
Attributes
amount[R]
amount: Rational, currency: String
currency[R]
amount: Rational, currency: String
Public Class Methods
conversion_rates(base_currency, rates)
click to toggle source
Example:
>> MoneyDt.conversion_rates("EUR", { "USD" => 1.11, "Bitcoin" => 0.0047 })
Arguments:
base_currency: String rates: Hash
# File lib/money_dt/conversions.rb, line 11 def self.conversion_rates(base_currency, rates) @@conversion_rates[base_currency] ||= {} @@conversion_rates[base_currency].merge!(rates) end
new(amount, currency)
click to toggle source
Example:
>> fifty_eur = MoneyDt.new(50, "EUR") #=> <MoneyDt amount:50/1 currency:EUR>
Arguments:
amount: Fixnum, Float currency: String
# File lib/money_dt.rb, line 12 def initialize(amount, currency) # The conversion to String is necessary as for a Float it isn't always valid to directly convert to Rational @amount = amount.to_s.to_r @currency = currency end
Public Instance Methods
*(value)
click to toggle source
Example:
>> MoneyDt.new(10, "USD") * 2 #=> <MoneyDt amount:20/1, currency:USD>
Arguments:
value: Fixnum, Float
# File lib/money_dt/arithmetics.rb, line 51 def *(value) MoneyDt.new(self.amount * value, self.currency) end
+(another)
click to toggle source
Example:
>> MoneyDt.conversion_rates("EUR", { "USD" => 1.10 }) >> MoneyDt.new(10, "EUR") + MoneyDt.new(10, "USD") #=> <MoneyDt amount:1909/100, currency:EUR>
Arguments:
another: MoneyDt
# File lib/money_dt/arithmetics.rb, line 10 def +(another) if self.currency == another.currency MoneyDt.new(self.amount + another.amount, self.currency) else another_converted = another.convert_to(self.currency) MoneyDt.new(self.amount + another_converted.amount, self.currency) end end
-(another)
click to toggle source
Example:
>> MoneyDt.conversion_rates("EUR", { "USD" => 1.10 }) >> MoneyDt.new(10, "EUR") - MoneyDt.new(10, "USD") #=> <MoneyDt amount:91/100, currency:EUR>
Arguments:
another: MoneyDt
# File lib/money_dt/arithmetics.rb, line 26 def -(another) if self.currency == another.currency MoneyDt.new(self.amount - another.amount, self.currency) else another_converted = another.convert_to(self.currency) MoneyDt.new(self.amount - another_converted.amount, self.currency) end end
/(value)
click to toggle source
Example:
>> MoneyDt.new(10, "USD") / 2 #=> <MoneyDt amount:5/1, currency:USD>
Arguments:
value: Fixnum, Float
# File lib/money_dt/arithmetics.rb, line 41 def /(value) MoneyDt.new(self.amount / value, self.currency) end
<=>(another)
click to toggle source
Two MoneyDt
objects are compared on the basis of their amount up to the cents
Arguments:
another: MoneyDt
# File lib/money_dt/comparisons.rb, line 9 def <=>(another) if self.currency == another.currency self.amount.round(2) <=> another.amount.round(2) else another_converted = another.convert_to(self.currency) self.amount.round(2) <=> another_converted.amount.round(2) end end
convert_to(new_currency)
click to toggle source
Example:
>> MoneyDt.conversion_rates("EUR", { "USD" => 1.11, "Bitcoin" => 0.0047 }) >> fifty_eur = MoneyDt.new(50, "EUR") >> fifty_eur.convert_to("USD") #=> <MoneyDt amount:555/10, currency:USD>
Arguments:
new_currency: String
# File lib/money_dt/conversions.rb, line 24 def convert_to(new_currency) if @@conversion_rates[self.currency] && (rate = @@conversion_rates[self.currency][new_currency]) # When converting to a base currency (rates are based on it) MoneyDt.new(self.amount * rate, new_currency) elsif @@conversion_rates[new_currency] && (rate = @@conversion_rates[new_currency][self.currency]) # When converting to another currency (rates aren't based on it) MoneyDt.new(self.amount * (1 / rate), new_currency) end end
inspect()
click to toggle source
Example:
>> fifty_eur = MoneyDt.new(50, "EUR") >> fifty_eur.inspect #=> "50.00 EUR"
# File lib/money_dt.rb, line 22 def inspect "#{'%.2f' % amount} #{currency}" end