Module: Measured::Arithmetic
- Included in:
- Measurable
- Defined in:
- lib/measured/arithmetic.rb
Instance Method Summary collapse
- #+(other) ⇒ Object
- #-(other) ⇒ Object
- #-@ ⇒ Object
- #coerce(other) ⇒ Object
- #scale(other) ⇒ Object
- #to_i ⇒ Object
Instance Method Details
#+(other) ⇒ Object
2 3 4 |
# File 'lib/measured/arithmetic.rb', line 2 def +(other) arithmetic_operation(other, :+) end |
#-(other) ⇒ Object
6 7 8 |
# File 'lib/measured/arithmetic.rb', line 6 def -(other) arithmetic_operation(other, :-) end |
#-@ ⇒ Object
10 11 12 |
# File 'lib/measured/arithmetic.rb', line 10 def -@ self.class.new(-self.value, self.unit) end |
#coerce(other) ⇒ Object
18 19 20 21 22 23 24 |
# File 'lib/measured/arithmetic.rb', line 18 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
14 15 16 |
# File 'lib/measured/arithmetic.rb', line 14 def scale(other) self.class.new(self.value * other, self.unit) end |
#to_i ⇒ Object
26 27 28 |
# File 'lib/measured/arithmetic.rb', line 26 def to_i raise TypeError, "#{self.class} cannot be converted to an integer" end |