Method: Quantify::Unit.ratio

Defined in:
lib/quantify/unit/unit.rb

.ratio(unit, other_unit) ⇒ Object

Returns an instance of the class Quantity which represents the ratio of two units. For example, the ratio of miles to kilometers is 1.609355, or there are 1.609355 km in 1 mile.

ratio = Unit.ratio :km, :mi         #=> <Quantify::Quantity:0xj9ab878a7>

ratio.to_s :name                    #=> "1.609344 kilometres per mile"

In other words the quantity represents the definition of one unit in terms of the other.



182
183
184
185
186
187
188
189
190
191
# File 'lib/quantify/unit/unit.rb', line 182

def self.ratio(unit,other_unit)
  unit = Unit.for unit
  other_unit = Unit.for other_unit
  unless unit.is_alternative_for? other_unit
    raise Exceptions::InvalidUnitError, "Units do not represent the same physical quantity"
  end
  new_unit = (unit / other_unit)
  value = 1/new_unit.factor
  Quantity.new(value, new_unit)
end