Module: Money::Arithmetic
- Included in:
- Money
- Defined in:
- lib/money/money/arithmetic.rb
Defined Under Namespace
Classes: CoercedNumeric
Instance Method Summary collapse
-
#%(val) ⇒ Money
Synonym for
#modulo. -
#*(value) ⇒ Money
Multiplies the monetary value with the given number and returns a new
Moneyobject with this monetary value and the same currency. -
#+(other) ⇒ Money
Returns a new Money object containing the sum of the two operands' monetary values.
-
#-(other) ⇒ Money
Returns a new Money object containing the sum of the two operands' monetary values.
-
#-@ ⇒ Money
Returns a money object with changed polarity.
-
#/(value) ⇒ Money, Float
Divides the monetary value with the given number and returns a new
Moneyobject with this monetary value and the same currency. -
#<=>(other) ⇒ Integer
Compares two Money objects.
-
#==(other) ⇒ Object
Uses Comparable's implementation but raises ArgumentError if non-zero numeric value is given.
-
#abs ⇒ Money
Return absolute value of self as a new Money object.
-
#coerce(other) ⇒ Array
Used to make Money instance handle the operations when arguments order is reversed.
-
#div(value) ⇒ Money, Float
Synonym for
#/. -
#divmod(val) ⇒ Array<Money,Money>, Array<Integer,Money>
Divide money by money or fixnum and return array containing quotient and modulus.
-
#eql?(other_money) ⇒ Boolean
Checks whether two Money objects have the same currency and the same amount.
-
#modulo(val) ⇒ Money
Equivalent to
self.divmod(val)[1]. -
#negative? ⇒ Boolean
Test if the amount is negative.
-
#nonzero? ⇒ Money?
Test if the money amount is non-zero.
-
#positive? ⇒ Boolean
Test if the amount is positive.
-
#remainder(val) ⇒ Money
If different signs
self.modulo(val) - valotherwiseself.modulo(val). -
#zero? ⇒ Boolean
Test if the money amount is zero.
Instance Method Details
#%(val) ⇒ Money
Synonym for #modulo.
246 247 248 |
# File 'lib/money/money/arithmetic.rb', line 246 def %(val) 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.
149 150 151 152 153 154 155 156 |
# File 'lib/money/money/arithmetic.rb', line 149 def *(value) value = value.value if value.is_a?(CoercedNumeric) if value.is_a? Numeric self.class.new(fractional * value, currency) else raise TypeError, "Can't multiply a #{self.class.name} by a #{value.class.name}'s value" end end |
#+(other) ⇒ 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.
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.
124 125 126 127 128 129 130 131 132 133 |
# File 'lib/money/money/arithmetic.rb', line 124 [:+, :-].each do |op| define_method(op) do |other| unless other.is_a?(Money) return self if other.zero? raise TypeError end other = other.exchange_to(currency) self.class.new(fractional.public_send(op, other.fractional), currency) end end |
#-(other) ⇒ 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.
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.
124 125 126 127 128 129 130 131 132 133 |
# File 'lib/money/money/arithmetic.rb', line 124 [:+, :-].each do |op| define_method(op) do |other| unless other.is_a?(Money) return self if other.zero? raise TypeError end other = other.exchange_to(currency) self.class.new(fractional.public_send(op, other.fractional), currency) end end |
#-@ ⇒ Money
Returns a money object with changed polarity.
18 19 20 |
# File 'lib/money/money/arithmetic.rb', line 18 def -@ self.class.new(-fractional, currency) 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.
173 174 175 176 177 178 179 180 |
# File 'lib/money/money/arithmetic.rb', line 173 def /(value) if value.is_a?(self.class) fractional / as_d(value.exchange_to(currency).fractional).to_f else raise TypeError, 'Can not divide by Money' if value.is_a?(CoercedNumeric) self.class.new(fractional / as_d(value), currency) end end |
#<=>(other) ⇒ Integer
Compares two Money objects. If money objects have a different currency it will attempt to convert the currency.
55 56 57 58 59 60 61 62 63 |
# File 'lib/money/money/arithmetic.rb', line 55 def <=>(other) unless other.is_a?(Money) return unless other.respond_to?(:zero?) && other.zero? return other.is_a?(CoercedNumeric) ? 0 <=> fractional : fractional <=> 0 end other = other.exchange_to(currency) fractional <=> other.fractional rescue Money::Bank::UnknownRate end |
#==(other) ⇒ Object
Uses Comparable's implementation but raises ArgumentError if non-zero numeric value is given.
67 68 69 70 71 72 |
# File 'lib/money/money/arithmetic.rb', line 67 def ==(other) if other.is_a?(Numeric) && !other.zero? raise ArgumentError, 'Money#== supports only zero numerics' end super end |
#abs ⇒ Money
Return absolute value of self as a new Money object.
276 277 278 |
# File 'lib/money/money/arithmetic.rb', line 276 def abs self.class.new(fractional.abs, currency) end |
#coerce(other) ⇒ Array
Used to make Money instance handle the operations when arguments order is reversed
308 309 310 |
# File 'lib/money/money/arithmetic.rb', line 308 def coerce(other) [self, CoercedNumeric.new(other)] end |
#div(value) ⇒ Money, Float
Synonym for #/.
191 192 193 |
# File 'lib/money/money/arithmetic.rb', line 191 def div(value) self / value end |
#divmod(val) ⇒ Array<Money,Money>, Array<Integer,Money>
Divide money by money or fixnum and return array containing quotient and modulus.
205 206 207 208 209 210 211 |
# File 'lib/money/money/arithmetic.rb', line 205 def divmod(val) if val.is_a?(Money) divmod_money(val) else divmod_other(val) end end |
#eql?(other_money) ⇒ Boolean
Checks whether two Money objects have the same currency and the same amount. If Money objects have a different currency it will only be true if the amounts are both zero. Checks against objects that are not Money or a subclass will always return false.
37 38 39 40 41 42 43 44 |
# File 'lib/money/money/arithmetic.rb', line 37 def eql?(other_money) if other_money.is_a?(Money) (fractional == other_money.fractional && currency == other_money.currency) || (fractional == 0 && other_money.fractional == 0) else false end end |
#modulo(val) ⇒ Money
Equivalent to self.divmod(val)[1]
235 236 237 |
# File 'lib/money/money/arithmetic.rb', line 235 def modulo(val) divmod(val)[1] end |
#negative? ⇒ Boolean
Test if the amount is negative. Returns true if the money amount is
less than 0, false otherwise.
96 97 98 |
# File 'lib/money/money/arithmetic.rb', line 96 def negative? fractional < 0 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?.
299 300 301 |
# File 'lib/money/money/arithmetic.rb', line 299 def nonzero? fractional != 0 ? self : nil end |
#positive? ⇒ Boolean
Test if the amount is positive. Returns true if the money amount is
greater than 0, false otherwise.
83 84 85 |
# File 'lib/money/money/arithmetic.rb', line 83 def positive? fractional > 0 end |
#remainder(val) ⇒ Money
If different signs self.modulo(val) - val otherwise self.modulo(val)
258 259 260 261 262 263 264 265 266 267 268 |
# File 'lib/money/money/arithmetic.rb', line 258 def remainder(val) if val.is_a?(Money) && currency != val.currency val = val.exchange_to(currency) end if (fractional < 0 && val < 0) || (fractional > 0 && val > 0) self.modulo(val) else self.modulo(val) - (val.is_a?(Money) ? val : self.class.new(val, currency)) end end |
#zero? ⇒ Boolean
Test if the money amount is zero.
287 288 289 |
# File 'lib/money/money/arithmetic.rb', line 287 def zero? fractional == 0 end |