module Measured::Arithmetic

Public Instance Methods

+(other) click to toggle source
# File lib/measured/arithmetic.rb, line 3
def +(other)
  arithmetic_operation(other, :+)
end
-(other) click to toggle source
# File lib/measured/arithmetic.rb, line 7
def -(other)
  arithmetic_operation(other, :-)
end
-@() click to toggle source
# File lib/measured/arithmetic.rb, line 11
def -@
  self.class.new(-self.value, self.unit)
end
coerce(other) click to toggle source
# File lib/measured/arithmetic.rb, line 19
def coerce(other)
  if other.is_a?(self.class)
    [other, self]
  else
    raise TypeError, "Cannot coerce #{other.class} to #{self.class}"
  end
end
scale(other) click to toggle source
# File lib/measured/arithmetic.rb, line 15
def scale(other)
  self.class.new(self.value * other, self.unit)
end
to_i() click to toggle source
# File lib/measured/arithmetic.rb, line 27
def to_i
  raise TypeError, "#{self.class} cannot be converted to an integer"
end

Private Instance Methods

arithmetic_operation(other, operator) click to toggle source
# File lib/measured/arithmetic.rb, line 33
def arithmetic_operation(other, operator)
  other, _ = coerce(other)
  self.class.new(self.value.public_send(operator, other.convert_to(self.unit).value), self.unit)
end