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.
@param [Numeric] value Number to multiply by.
@return [Money] The resulting money.
@raise [ArgumentError] If value is NOT a number.
@example
Money.new(100) * 2 #=> #<Money @fractional=200>
# File lib/money/money/arithmetic.rb, line 124 def *(value) if value.is_a? Numeric Money.new(fractional * value, currency) else raise ArgumentError, "Can't multiply a Money by a #{value.class.name}'s value" end end
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.
@param [Money] other_money Other Money object to add.
@return [Money]
@example
Money.new(100) + Money.new(100) #=> #<Money @fractional=200>
# File lib/money/money/arithmetic.rb, line 89 def +(other_money) other_money = other_money.exchange_to(currency) Money.new(fractional + other_money.fractional, currency) end
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.
@param [Money] other_money Other Money object to subtract.
@return [Money]
@example
Money.new(100) - Money.new(99) #=> #<Money @fractional=1>
# File lib/money/money/arithmetic.rb, line 105 def -(other_money) other_money = other_money.exchange_to(currency) Money.new(fractional - other_money.fractional, currency) end
Returns a money object with changed polarity.
@return [Money]
@example
- Money.new(100) #=> #<Money @fractional=-100>
# File lib/money/money/arithmetic.rb, line 10 def -@ Money.new(-fractional, currency) end
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.
@param [Money, Numeric] value Number to divide by.
@return [Money] The resulting money if you divide Money by a number. @return [Float] The resulting number if you divide Money by a Money.
@example
Money.new(100) / 10 #=> #<Money @fractional=10> Money.new(100) / Money.new(10) #=> 10.0
# File lib/money/money/arithmetic.rb, line 147 def /(value) if value.is_a?(Money) fractional / as_d(value.exchange_to(currency).fractional).to_f else Money.new(fractional / as_d(value), currency) end end
# File lib/money/money/arithmetic.rb, line 43 def <=>(val) val = val.to_money unless fractional == 0 || val.fractional == 0 || currency == val.currency val = val.exchange_to(currency) end fractional <=> val.fractional rescue NoMethodError raise ArgumentError, "Comparison of #{self.class} with #{val.inspect} failed" end
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.
@param [Money] other_money Value to compare with.
@return [Boolean]
@example
Money.new(100) == Money.new(101) #=> false Money.new(100) == Money.new(100) #=> true
# File lib/money/money/arithmetic.rb, line 25 def ==(other_money) other_money = other_money.to_money fractional == other_money.fractional && currency == other_money.currency rescue NoMethodError false end
Return absolute value of self as a new Money object.
@return [Money]
@example
Money.new(-100).abs #=> #<Money @fractional=100>
# File lib/money/money/arithmetic.rb, line 253 def abs Money.new(fractional.abs, currency) end
Used to make Money instance handle the operations when arguments order is reversed @return [Array]
@example
2 * Money.new(10) #=> #<Money @fractional=20>
# File lib/money/money/arithmetic.rb, line 285 def coerce(other) [self, other] end
Divide money by money or fixnum and return array containing quotient and modulus.
@param [Money, Fixnum] val Number to divmod by.
@return [Array<Money,Money>,Array<Fixnum,Money>]
@example
Money.new(100).divmod(9) #=> [#<Money @fractional=11>, #<Money @fractional=1>] Money.new(100).divmod(Money.new(9)) #=> [11, #<Money @fractional=1>]
# File lib/money/money/arithmetic.rb, line 178 def divmod(val) if val.is_a?(Money) divmod_money(val) else divmod_other(val) end end
Synonymous with +#==+.
@param [Money] other_money Value to compare with.
@return [Money]
@see #==
# File lib/money/money/arithmetic.rb, line 39 def eql?(other_money) self == other_money end
Equivalent to +self.divmod(val)+
@param [Money, Fixnum] val Number take modulo with.
@return [Money]
@example
Money.new(100).modulo(9) #=> #<Money @fractional=1> Money.new(100).modulo(Money.new(9)) #=> #<Money @fractional=1>
# File lib/money/money/arithmetic.rb, line 212 def modulo(val) divmod(val)[1] end
Test if the amount is negative. Returns true if the money amount is less than 0, false otherwise.
@return [Boolean]
@example
Money.new(-1).negative? #=> true Money.new(0).negative? #=> false Money.new(1).negative? #=> false
# File lib/money/money/arithmetic.rb, line 75 def negative? fractional < 0 end
Test if the money amount is non-zero. Returns this money object if it is non-zero, or nil otherwise, like +Numeric#nonzero?+.
@return [Money, nil]
@example
Money.new(100).nonzero? #=> #<Money @fractional=100> Money.new(0).nonzero? #=> nil
# File lib/money/money/arithmetic.rb, line 276 def nonzero? fractional != 0 ? self : nil end
Test if the amount is positive. Returns true if the money amount is greater than 0, false otherwise.
@return [Boolean]
@example
Money.new(1).positive? #=> true Money.new(0).positive? #=> false Money.new(-1).positive? #=> false
# File lib/money/money/arithmetic.rb, line 62 def positive? fractional > 0 end
If different signs +self.modulo(val) - val+ otherwise +self.modulo(val)+
@param [Money, Fixnum] val Number to rake remainder with.
@return [Money]
@example
Money.new(100).remainder(9) #=> #<Money @fractional=1>
# File lib/money/money/arithmetic.rb, line 235 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 : Money.new(val, currency)) end end
Generated with the Darkfish Rdoc Generator 2.