Module: Measured::Arithmetic

Included in:
Measurable
Defined in:
lib/measured/arithmetic.rb

Instance Method Summary collapse

Instance Method Details

#+(other) ⇒ Object



3
4
5
# File 'lib/measured/arithmetic.rb', line 3

def +(other)
  arithmetic_operation(other, :+)
end

#-(other) ⇒ Object



7
8
9
# File 'lib/measured/arithmetic.rb', line 7

def -(other)
  arithmetic_operation(other, :-)
end

#-@Object



11
12
13
# File 'lib/measured/arithmetic.rb', line 11

def -@
  self.class.new(-self.value, self.unit)
end

#coerce(other) ⇒ Object



19
20
21
22
23
24
25
# 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) ⇒ Object



15
16
17
# File 'lib/measured/arithmetic.rb', line 15

def scale(other)
  self.class.new(self.value * other, self.unit)
end

#to_iObject

Raises:

  • (TypeError)


27
28
29
# File 'lib/measured/arithmetic.rb', line 27

def to_i
  raise TypeError, "#{self.class} cannot be converted to an integer"
end