lib/quantify/quantity.rb
# Converts self into a quantity using the unit provided as an argument. The
# new unit must represent the same physical quantity, i.e. have the same
# dimensions, e.g.
#
#   Quantity.parse("12 yd").to(:foot).to_s          #=> "36 ft"
#
#   1000.kilogram.to(:tonne).to_s                   #=> "1 t"
#
# For compound units, it is permissable to pass in units which are same dimension of at
# least one base unit. 
#
# #method_missing provides some syntactic sugar for the new unit to
# be provided as part of the method name, based around /to_(<unit>)/, e.g.
#
#   200.cm.to_metre.to_s                   #=> "1 t"
#
# The unit value is converted to the corresponding value for the same quantity
# in terms of the new unit.
#
Converts self into a quantity using the unit provided as an argument. The
new unit must represent the same physical quantity, i.e. have the same
dimensions, e.g.





For compound units, it is permissable to pass in units which are same dimension of at
least one base unit.

method_missing provides some syntactic sugar for the new unit to
be provided as part of the method name, based around /to_()/, e.g.



The unit value is converted to the corresponding value for the same quantity
in terms of the new unit.
Converts self into a quantity using the unit provided as an argument . The new unit must represent the same physical quantity i . e . have the same dimensions e . g . For compound units it is permissable to pass in units which are same dimension of at least one base unit . method_missing provides some syntactic sugar for the new unit to be provided as part of the method name based around / to_ () / e . g . The unit value is converted to the corresponding value for the same quantity in terms of the new unit .
def to(new_unit)
      new_unit = Unit.for(new_unit)
      if Quantity.is_basic_conversion_with_scalings?(self,new_unit)
        Quantity.new(@value,@unit).conversion_with_scalings! new_unit
      elsif self.unit.is_alternative_for? new_unit
        Quantity.new(@value,@unit).convert_to_equivalent_unit! new_unit
      elsif self.unit.is_compound_unit?
        Quantity.new(@value,@unit).convert_compound_unit_to_non_equivalent_unit! new_unit
      else
        raise Exceptions::InvalidArgumentError, "Cannot convert #{self.unit.to_s} quantity into #{new_unit.to_s}. Units are incompatible"
      end      
    end
def to ( new_unit ) new_unit = Unit . for ( new_unit ) if Quantity . is_basic_conversion_with_scalings? ( self , new_unit ) Quantity . new ( @value , @unit ) . conversion_with_scalings! new_unit elsif self . unit . is_alternative_for? new_unit Quantity . new ( @value , @unit ) . convert_to_equivalent_unit! new_unit elsif self . unit . is_compound_unit? Quantity . new ( @value , @unit ) . convert_compound_unit_to_non_equivalent_unit! new_unit else raise Exceptions :: InvalidArgumentError , "Cannot convert #{self.unit.to_s} quantity into #{new_unit.to_s}. Units are incompatible" end end
Converts self into a quantity using the unit provided as an argument.
Converts self into a quantity using the unit provided as an argument .
new_unit
<not_specific>