class MoneyMaahd
This class is used to convert currencies and do some simple math.
Attributes
amount[RW]
reader and writer for both class and instance variables
base_currency[RW]
reader and writer for both class and instance variables
currency[RW]
reader and writer for both class and instance variables
rates[RW]
reader and writer for both class and instance variables
Public Class Methods
conversion_rates(base_currency, rates = {})
click to toggle source
define class method for assigning base currency and exchange rates
# File lib/money_maahd.rb, line 20 def self.conversion_rates(base_currency, rates = {}) @@base_currency = base_currency @@rates = rates end
new(amount, currency)
click to toggle source
initialize with instance variables
# File lib/money_maahd.rb, line 9 def initialize(amount, currency) @amount = amount @currency = currency end
Public Instance Methods
*(right)
click to toggle source
override the multiply operator
# File lib/money_maahd.rb, line 55 def *(right) product = @amount * right MoneyMaahd.new('%.2f' % [product], @currency) end
+(right)
click to toggle source
override the plus operator
# File lib/money_maahd.rb, line 35 def +(right) right_in_base_currency = right.convert_to(@@base_currency) sum = @amount + right_in_base_currency.amount.to_f MoneyMaahd.new('%.2f' % [sum], @@base_currency) end
-(right)
click to toggle source
override the subtraction operator
# File lib/money_maahd.rb, line 42 def -(right) right_in_base_currency = right.convert_to(@@base_currency) difference = @amount - right_in_base_currency.amount.to_f MoneyMaahd.new('%.2f' % [difference], @@base_currency) end
/(right)
click to toggle source
override the division operator
# File lib/money_maahd.rb, line 49 def /(right) quotient = @amount / right MoneyMaahd.new('%.2f' % [quotient], @currency) end
<(right)
click to toggle source
override the is less than operator
# File lib/money_maahd.rb, line 83 def <(right) if self.currency == right.currency self.amount < right.amount else #if left is not base currency left = self.convert_to(@@base_currency) left_amount = left.amount.to_f left_amount < right.amount end end
==(right)
click to toggle source
override the is equal operator
# File lib/money_maahd.rb, line 61 def ==(right) if right.currency == @currency @amount == right.amount else left = self.convert_to(@@base_currency) left_amount = left.amount.to_f left_amount == right.amount end end
>(right)
click to toggle source
override the is greater than operator
# File lib/money_maahd.rb, line 72 def >(right) if self.currency == right.currency self.amount > right.amount else # if left is not base currency left = self.convert_to(@@base_currency) left_amount = left.amount.to_f left_amount > right.amount end end
convert_to(currency)
click to toggle source
converts to a different currency
# File lib/money_maahd.rb, line 26 def convert_to(currency) if currency != @@base_currency MoneyMaahd.new('%.2f' % [@amount * @@rates[currency]], currency) else MoneyMaahd.new('%.2f' % [@amount.to_f / @@rates[@currency]], currency) end end
inspect()
click to toggle source
override the inspect method
# File lib/money_maahd.rb, line 15 def inspect '%.2f' % [@amount] + " " + @currency end