Module: Measured::Arithmetic
Instance Method Summary collapse
- #+(other) ⇒ Object
- #-(other) ⇒ Object
- #-@ ⇒ Object
- #coerce(other) ⇒ Object
- #nonzero? ⇒ Boolean
- #scale(other) ⇒ Object
- #to_i ⇒ Object
Instance Method Details
#+(other) ⇒ Object
6 7 8 |
# File 'lib/measured/arithmetic.rb', line 6 def +(other) arithmetic_operation(other, :+) end |
#-(other) ⇒ Object
10 11 12 |
# File 'lib/measured/arithmetic.rb', line 10 def -(other) arithmetic_operation(other, :-) end |
#-@ ⇒ Object
14 15 16 |
# File 'lib/measured/arithmetic.rb', line 14 def -@ self.class.new(-self.value, self.unit) end |
#coerce(other) ⇒ Object
22 23 24 25 26 27 28 29 30 |
# File 'lib/measured/arithmetic.rb', line 22 def coerce(other) if other.is_a?(self.class) [other, self] elsif other.is_a?(Numeric) && other.zero? [self.class.new(other, self.unit), self] else raise TypeError, "Cannot coerce #{other.class} to #{self.class}" end end |
#nonzero? ⇒ Boolean
36 37 38 |
# File 'lib/measured/arithmetic.rb', line 36 def nonzero? value.nonzero? ? self : false end |
#scale(other) ⇒ Object
18 19 20 |
# File 'lib/measured/arithmetic.rb', line 18 def scale(other) self.class.new(self.value * other, self.unit) end |
#to_i ⇒ Object
32 33 34 |
# File 'lib/measured/arithmetic.rb', line 32 def to_i raise TypeError, "#{self.class} cannot be converted to an integer" end |