class Money

Attributes

amount[RW]
currency[RW]

Public Class Methods

conversion_rates(currency_from, option={}) click to toggle source
# File lib/curconarith.rb, line 4
def self.conversion_rates(currency_from, option={}) #class method
  @@hash_of_currency   = {}
  @@currency_of_system = currency_from

  option.each do |currency_to, rate|
    @@hash_of_currency[currency_to] = rate
  end
    
end
new(amount, currency) click to toggle source
# File lib/curconarith.rb, line 14
def initialize(amount, currency)
  self.amount   = amount
  self.currency = currency
end

Public Instance Methods

*(other) click to toggle source

Arithmetics

# File lib/curconarith.rb, line 59
def *(other)
  if other.is_a? Numeric
  self.class.new(other*amount, currency)
  else
    raise TypeError, "Please do not do that again!"
  end
end
-@() click to toggle source
# File lib/curconarith.rb, line 49
def -@
  self.class.new(-amount, currency)
end
/(other) click to toggle source
# File lib/curconarith.rb, line 67
def /(other)
  if other.is_a?(Money)
    if currency == other.currency
      amount/other.amount
    else
      self.convert_to_currency_of_system
      other.convert_to_currency_of_system
      self/other
    end
  elsif other.is_a?(Numeric)
    self.class.new((amount/other.to_f), currency)
  else
    raise TypeError, "How on Earth you can do that"
  end
end
abs() click to toggle source
# File lib/curconarith.rb, line 53
def abs
  self.class.new(amount.abs, currency)
end
convert_to(currency_to) click to toggle source

convertation

# File lib/curconarith.rb, line 20
def convert_to(currency_to) #instance method
  if @@hash_of_currency[currency_to] && self.currency == @@currency_of_system
   self.amount   = amount*@@hash_of_currency[currency_to]
   self.currency = currency_to
   self
  elsif @@hash_of_currency[currency_to] 
 # convert first to EUR that to given currency
   self.convert_to_currency_of_system
   self.convert_to(currency_to)
  else
   raise TypeError, 
   "There is no chance that you can convert #{self.inspect} to #{currency_to}"
  end
end
convert_to_currency_of_system() click to toggle source
# File lib/curconarith.rb, line 35
def convert_to_currency_of_system
  if self.currency == @@currency_of_system
    self.amount   
    self.currency
  else
    self.amount   = amount*(1/@@hash_of_currency[self.currency])
    self.currency = @@currency_of_system
  end
end
inspect() click to toggle source
# File lib/curconarith.rb, line 45
def inspect
  "\"#{@amount} #{@currency}\""    
end