module Money::Arithmetic

def %(val)

Other tags:
    See: #modulo -

Returns:
  • (Money) -

Parameters:
  • val (Money, Integer) -- Number take modulo with.
def %(val)
  modulo(val)
end

def *(value)

Raises:
  • (TypeError) - If +value+ is NOT a number.

Returns:
  • (Money) - The resulting money.

Parameters:
  • value (Numeric) -- Number to multiply by.
def *(value)
  value = value.value if value.is_a?(CoercedNumeric)
  if value.is_a? Numeric
    dup_with(fractional: fractional * value)
  else
    raise TypeError, "Can't multiply a #{self.class.name} by a #{value.class.name}'s value"
  end
end

def -@

Returns:
  • (Money) -
def -@
  dup_with(fractional: -fractional)
end

def /(value)

Returns:
  • (Float) - The resulting number if you divide Money by a Money.
  • (Money) - The resulting money if you divide Money by a number.

Parameters:
  • value (Money, Numeric) -- Number to divide by.
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)
    dup_with(fractional: fractional / as_d(value))
  end
end

def <=>(other)

Raises:
  • (TypeError) - when other object is not Money

Returns:
  • (Integer) -

Parameters:
  • other (Money) -- Value to compare with.
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
  # Always allow comparison with zero
  if zero? || other.zero?
    return fractional <=> other.fractional
  end
  other = other.exchange_to(currency)
  fractional <=> other.fractional
rescue Money::Bank::UnknownRate
end

def ==(other)

numeric value is given.
Uses Comparable's implementation but raises ArgumentError if non-zero
def ==(other)
  if other.is_a?(Numeric) && !other.zero?
    raise ArgumentError, 'Money#== supports only zero numerics'
  end
  super
end

def abs

Returns:
  • (Money) -
def abs
  dup_with(fractional: fractional.abs)
end

def coerce(other)

Returns:
  • (Array) -
def coerce(other)
  [self, CoercedNumeric.new(other)]
end

def div(value)

Other tags:
    See: #/ -

Returns:
  • (Float) - The resulting number if you divide Money by a Money.
  • (Money) - The resulting money if you divide Money by a number.

Parameters:
  • value (Money, Numeric) -- Number to divide by.
def div(value)
  self / value
end

def divmod(val)

Returns:
  • (Array, Array) -

Parameters:
  • val (Money, Integer) -- Number to divmod by.
def divmod(val)
  if val.is_a?(Money)
    divmod_money(val)
  else
    divmod_other(val)
  end
end

def divmod_money(val)

def divmod_money(val)
  cents = val.exchange_to(currency).cents
  quotient, remainder = fractional.divmod(cents)
  [quotient, dup_with(fractional: remainder)]
end

def divmod_other(val)

def divmod_other(val)
  quotient, remainder = fractional.divmod(as_d(val))
  [dup_with(fractional: quotient), dup_with(fractional: remainder)]
end

def eql?(other_money)

Returns:
  • (Boolean) -

Parameters:
  • other_money (Money) -- Value to compare with.
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

def modulo(val)

Returns:
  • (Money) -

Parameters:
  • val (Money, Integer) -- Number take modulo with.
def modulo(val)
  divmod(val)[1]
end

def negative?

Returns:
  • (Boolean) -
def negative?
  fractional < 0
end

def nonzero?

Returns:
  • (Money, nil) -
def nonzero?
  fractional != 0 ? self : nil
end

def positive?

Returns:
  • (Boolean) -
def positive?
  fractional > 0
end

def remainder(val)

Returns:
  • (Money) -

Parameters:
  • val (Money, Integer) -- Number to rake remainder with.
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 : dup_with(fractional: val))
  end
end

def zero?

Returns:
  • (Boolean) -
def zero?
  fractional == 0
end