class Metamorphosis::UnitValue
Attributes
exponent[R]
unit_symbol[R]
value[R]
Public Class Methods
new(unit_symbol, value, exponent = 1.0)
click to toggle source
# File lib/metamorphosis/unit_value.rb, line 7 def initialize(unit_symbol, value, exponent = 1.0) @unit_symbol = unit_symbol.to_sym @value = value @exponent = exponent.to_f end
Public Instance Methods
<=>(other_unit)
click to toggle source
# File lib/metamorphosis/unit_value.rb, line 25 def <=>(other_unit) unless other_unit.is_a?(UnitValue) || other_unit.is_a?(Numeric) raise ArgumentError, "Unable to compare #{self.to_s} with #{other_unit.class}" end # Assume numeric values are of the same unit type return self.to_f <=> other_unit if other_unit.is_a?(Numeric) # Convert the unit if they're not the same unit already unless @unit_symbol == other_unit.unit_symbol other_unit = other_unit.to(@unit_symbol) end self.to_f <=> other_unit.to_f end
method_missing(method_name, *args, &block)
click to toggle source
Calls superclass method
# File lib/metamorphosis/unit_value.rb, line 48 def method_missing method_name, *args, &block self.to(method_name) || super( method_name, *args, &block ) end
to(unit_symbol = nil)
click to toggle source
# File lib/metamorphosis/unit_value.rb, line 42 def to(unit_symbol = nil) return self unless unit_symbol Metamorphosis.engine.convert_to(unit_symbol, self) end
Also aliased as: as
to_f()
click to toggle source
# File lib/metamorphosis/unit_value.rb, line 17 def to_f @exponent * @value end
to_i()
click to toggle source
# File lib/metamorphosis/unit_value.rb, line 21 def to_i to_f.to_i end
to_s()
click to toggle source
# File lib/metamorphosis/unit_value.rb, line 13 def to_s "#{self.to_f} #{unit_symbol}" end