Module: Money::Arithmetic
- Included in:
- Money
- Defined in:
- lib/money/money/arithmetic.rb
Instance Method Summary collapse
-
#%(val) ⇒ Money
Synonym for
#modulo
. -
#*(value) ⇒ Money
Multiplies the monetary value with the given number and returns a new
Money
object with this monetary value and the same currency. -
#+(other_money) ⇒ Money
Returns a new Money object containing the sum of the two operands’ monetary values.
-
#-(other_money) ⇒ Money
Returns a new Money object containing the difference between the two operands’ monetary values.
-
#/(value) ⇒ Money, Float
Divides the monetary value with the given number and returns a new
Money
object with this monetary value and the same currency. - #<=>(other_money) ⇒ Object
-
#==(other_money) ⇒ Boolean
Checks whether two money objects have the same currency and the same amount.
-
#abs ⇒ Money
Return absolute value of self as a new Money object.
-
#div(value) ⇒ Money, Float
Synonym for
#/
. -
#divmod(val) ⇒ Array<Money,Money>, Array<Fixnum,Money>
Divide money by money or fixnum and return array containing quotient and modulus.
-
#eql?(other_money) ⇒ Money
Synonymous with #==.
-
#modulo(val) ⇒ Money
Equivalent to self.divmod(val).
-
#nonzero? ⇒ Money?
Test if the money amount is non-zero.
-
#remainder(val) ⇒ Money
If different signs self.modulo(val) - val otherwise self.modulo(val).
-
#zero? ⇒ Boolean
Test if the money amount is zero.
Instance Method Details
#%(val) ⇒ Money
Synonym for #modulo
.
188 189 190 |
# File 'lib/money/money/arithmetic.rb', line 188 def %(val) self.modulo(val) end |
#*(value) ⇒ Money
Multiplies the monetary value with the given number and returns a new Money
object with this monetary value and the same currency.
Note that you can’t multiply a Money object by an other Money
object.
99 100 101 102 103 104 105 |
# File 'lib/money/money/arithmetic.rb', line 99 def *(value) if value.is_a?(Money) raise ArgumentError, "Can't multiply a Money by a Money" else Money.new(cents * value, currency) end end |
#+(other_money) ⇒ Money
Returns a new Money object containing the sum of the two operands’ monetary values. If other_money
has a different currency then its monetary value is automatically exchanged to this object’s currency using exchange_to
.
58 59 60 61 62 63 64 |
# File 'lib/money/money/arithmetic.rb', line 58 def +(other_money) if currency == other_money.currency Money.new(cents + other_money.cents, other_money.currency) else Money.new(cents + other_money.exchange_to(currency).cents, currency) end end |
#-(other_money) ⇒ Money
Returns a new Money object containing the difference between the two operands’ monetary values. If other_money
has a different currency then its monetary value is automatically exchanged to this object’s currency using exchange_to
.
77 78 79 80 81 82 83 |
# File 'lib/money/money/arithmetic.rb', line 77 def -(other_money) if currency == other_money.currency Money.new(cents - other_money.cents, other_money.currency) else Money.new(cents - other_money.exchange_to(currency).cents, currency) end end |
#/(value) ⇒ Money, Float
Divides the monetary value with the given number and returns a new Money
object with this monetary value and the same currency. Can also divide by another Money
object to get a ratio.
Money/Numeric
returns Money
. Money/Money
returns Float
.
122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/money/money/arithmetic.rb', line 122 def /(value) if value.is_a?(Money) if currency == value.currency (cents / BigDecimal.new(value.cents.to_s)).to_f else (cents / BigDecimal(value.exchange_to(currency).cents.to_s)).to_f end else Money.new(cents / value, currency) end end |
#<=>(other_money) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/money/money/arithmetic.rb', line 35 def <=>(other_money) if other_money.respond_to?(:to_money) other_money = other_money.to_money if self.currency == other_money.currency cents <=> other_money.cents else cents <=> other_money.exchange_to(currency).cents end else raise ArgumentError, "Comparison of #{self.class} with #{other_money.inspect} failed" end end |
#==(other_money) ⇒ Boolean
Checks whether two money objects have the same currency and the same amount. Checks against money objects with a different currency and checks against objects that do not respond to #to_money will always return false.
15 16 17 18 19 20 21 22 |
# File 'lib/money/money/arithmetic.rb', line 15 def ==(other_money) if other_money.respond_to?(:to_money) other_money = other_money.to_money cents == other_money.cents && self.currency == other_money.currency else false end end |
#abs ⇒ Money
Return absolute value of self as a new Money object.
218 219 220 |
# File 'lib/money/money/arithmetic.rb', line 218 def abs Money.new(self.cents.abs, self.currency) end |
#div(value) ⇒ Money, Float
Synonym for #/
.
143 144 145 |
# File 'lib/money/money/arithmetic.rb', line 143 def div(value) self / value end |
#divmod(val) ⇒ Array<Money,Money>, Array<Fixnum,Money>
Divide money by money or fixnum and return array containing quotient and modulus.
157 158 159 160 161 162 163 164 165 166 |
# File 'lib/money/money/arithmetic.rb', line 157 def divmod(val) if val.is_a?(Money) a = self.cents b = self.currency == val.currency ? val.cents : val.exchange_to(self.currency).cents q, m = a.divmod(b) return [q, Money.new(m, self.currency)] else return [self.div(val), Money.new(self.cents.modulo(val), self.currency)] end end |
#eql?(other_money) ⇒ Money
Synonymous with #==.
31 32 33 |
# File 'lib/money/money/arithmetic.rb', line 31 def eql?(other_money) self == other_money end |
#modulo(val) ⇒ Money
Equivalent to self.divmod(val)
177 178 179 |
# File 'lib/money/money/arithmetic.rb', line 177 def modulo(val) self.divmod(val)[1] end |
#nonzero? ⇒ Money?
Test if the money amount is non-zero. Returns this money object if it is non-zero, or nil otherwise, like Numeric#nonzero?.
241 242 243 |
# File 'lib/money/money/arithmetic.rb', line 241 def nonzero? cents != 0 ? self : nil end |
#remainder(val) ⇒ Money
If different signs self.modulo(val) - val otherwise self.modulo(val)
200 201 202 203 204 205 206 207 208 209 210 |
# File 'lib/money/money/arithmetic.rb', line 200 def remainder(val) a, b = self, val b = b.exchange_to(a.currency) if b.is_a?(Money) and a.currency != b.currency a_sign, b_sign = :pos, :pos a_sign = :neg if a.cents < 0 b_sign = :neg if (b.is_a?(Money) and b.cents < 0) or (b < 0) return a.modulo(b) if a_sign == b_sign a.modulo(b) - (b.is_a?(Money) ? b : Money.new(b, a.currency)) end |
#zero? ⇒ Boolean
Test if the money amount is zero.
229 230 231 |
# File 'lib/money/money/arithmetic.rb', line 229 def zero? cents == 0 end |