Sha256: a207b5d5e5bd7802c20e181d41a315c13f1746cc98687adf8048a04b339ed033
Contents?: true
Size: 1007 Bytes
Versions: 4
Compression:
Stored size: 1007 Bytes
Contents
# frozen_string_literal: true module Measured::Arithmetic extend Forwardable def_delegators :@value, :zero?, :positive?, :negative?, :finite?, :infinite? def +(other) arithmetic_operation(other, :+) end def -(other) arithmetic_operation(other, :-) end def -@ self.class.new(-self.value, self.unit) end def scale(other) self.class.new(self.value * other, self.unit) end 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 def to_i raise TypeError, "#{self.class} cannot be converted to an integer" end def nonzero? value.nonzero? ? self : false end private 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 end
Version data entries
4 entries across 4 versions & 1 rubygems
Version | Path |
---|---|
measured-3.1.0 | lib/measured/arithmetic.rb |
measured-3.0.0 | lib/measured/arithmetic.rb |
measured-2.8.2 | lib/measured/arithmetic.rb |
measured-2.8.1 | lib/measured/arithmetic.rb |