lib/measured/arithmetic.rb in measured-1.6.0 vs lib/measured/arithmetic.rb in measured-2.0.0.pre1

- old
+ new

@@ -5,33 +5,32 @@ def -(other) arithmetic_operation(other, :-) end - def *(other) - arithmetic_operation(other, :*) + def -@ + self.class.new(-self.value, self.unit) end - def /(other) - arithmetic_operation(other, :/) + def scale(other) + self.class.new(self.value * other, self.unit) end - def -@ - self.class.new(-self.value, self.unit) + def coerce(other) + if other.is_a?(self.class) + [other, self] + else + raise TypeError, "Cannot coerce #{other.class} to #{self.class}" + end end - def coerce(other) - [self, other] + def to_i + raise TypeError, "#{self.class} cannot be converted to an integer" end private def arithmetic_operation(other, operator) - if other.is_a?(self.class) - self.class.new(self.value.send(operator, other.convert_to(self.unit).value), self.unit) - elsif other.is_a?(Numeric) - self.class.new(self.value.send(operator, other), self.unit) - else - raise TypeError, "Invalid operation #{ operator } between #{ self.class } to #{ other.class }" - end + other, _ = coerce(other) + self.class.new(self.value.public_send(operator, other.convert_to(self.unit).value), self.unit) end end