Class: Money
- Inherits:
-
Object
- Object
- Money
- Defined in:
- lib/money.rb
Instance Attribute Summary collapse
-
#amount ⇒ Object
Returns the value of attribute amount.
-
#conversions ⇒ Object
Returns the value of attribute conversions.
-
#currency ⇒ Object
Returns the value of attribute currency.
Instance Method Summary collapse
- #*(other) ⇒ Object
- #+(other) ⇒ Object
- #-(other) ⇒ Object
- #/(other) ⇒ Object
- #<(other) ⇒ Object
- #==(other) ⇒ Object
- #>(other) ⇒ Object
- #convert_to(currency) ⇒ Object
-
#initialize(amount = 0, currency = 'EUR') ⇒ Money
constructor
A new instance of Money.
Constructor Details
#initialize(amount = 0, currency = 'EUR') ⇒ Money
Returns a new instance of Money.
8 9 10 11 12 13 14 15 |
# File 'lib/money.rb', line 8 def initialize(amount = 0, currency = 'EUR') @currency = currency @amount = amount @conversions = { 'USD' => 1.11, 'Bitcoin' => 0.0047 } end |
Instance Attribute Details
#amount ⇒ Object
Returns the value of attribute amount.
6 7 8 |
# File 'lib/money.rb', line 6 def amount @amount end |
#conversions ⇒ Object
Returns the value of attribute conversions.
6 7 8 |
# File 'lib/money.rb', line 6 def conversions @conversions end |
#currency ⇒ Object
Returns the value of attribute currency.
6 7 8 |
# File 'lib/money.rb', line 6 def currency @currency end |
Instance Method Details
#*(other) ⇒ Object
58 59 60 61 62 63 64 65 66 67 |
# File 'lib/money.rb', line 58 def *(other) if other.is_a?(MoneyHdamico) other.convert_to(currency) if other.currency != currency MoneyHdamico.new(amount * other.amount, currency) elsif other.is_a?(Numeric) MoneyHdamico.new(amount * other, currency) else raise 'Cannot convert String into Numeric' end end |
#+(other) ⇒ Object
36 37 38 39 40 41 42 43 44 45 |
# File 'lib/money.rb', line 36 def +(other) if other.is_a?(MoneyHdamico) other.convert_to(currency) if other.currency != currency MoneyHdamico.new(amount + other.amount, currency) elsif other.is_a?(Numeric) MoneyHdamico.new(amount + other, currency) else raise 'Cannot convert String into Numeric' end end |
#-(other) ⇒ Object
25 26 27 28 29 30 31 32 33 34 |
# File 'lib/money.rb', line 25 def -(other) if other.is_a?(MoneyHdamico) other.convert_to(currency) if other.currency != currency MoneyHdamico.new(amount - other.amount, currency) elsif other.is_a?(Numeric) MoneyHdamico.new(amount - other, currency) else raise 'Cannot convert String into Numeric' end end |
#/(other) ⇒ Object
47 48 49 50 51 52 53 54 55 56 |
# File 'lib/money.rb', line 47 def /(other) if other.is_a?(MoneyHdamico) other.convert_to(currency) if other.currency != currency MoneyHdamico.new(amount / other.amount, currency) elsif other.is_a?(Numeric) MoneyHdamico.new(amount / other, currency) else raise 'Cannot convert String into Numeric' end end |
#<(other) ⇒ Object
69 70 71 72 73 74 75 |
# File 'lib/money.rb', line 69 def <(other) if other.is_a?(MoneyHdamico) other.convert_to(currency) if other.currency != currency return true if amount < other.amount end false end |
#==(other) ⇒ Object
85 86 87 88 89 90 |
# File 'lib/money.rb', line 85 def ==(other) if other.is_a?(MoneyHdamico) return true if amount == other.amount && currency == other.currency end false end |
#>(other) ⇒ Object
77 78 79 80 81 82 83 |
# File 'lib/money.rb', line 77 def >(other) if other.is_a?(MoneyHdamico) other.convert_to(currency) if other.currency != currency return true if amount > other.amount end false end |
#convert_to(currency) ⇒ Object
17 18 19 20 21 22 23 |
# File 'lib/money.rb', line 17 def convert_to(currency) return raise 'Incorrect currency' unless self.currency != currency || @conversions.include?(currency) self.amount = (@conversions[currency] * amount).round(2) self.currency = currency self end |