Class | Units::Unit |
In: |
lib/facets/more/units.rb
|
Parent: | Object |
This class represents a Unit. A Unit uses a given Converter with a number of registered Units in which it can be expressed. A Unit is the product of the powers of other Units. In principle, these need not be integer powers, but this may cause problems with rounding. The following code for example returns false:
Unit.new(:m => 0.1) * Unit.new(:m => 0.2) == Unit.new(:m => 0.3)
Units can be multiplied and divided, or raised to a given power. This can only be done when both units use the same Converter. As an extra, 1 can be divided by a Unit.
Examples:
Unit.new(:mi => 1, :s => -1) ** 2 # => mi**2/s**2 Unit.new(:mi => 1, :s => -1) * Unit.new(:s => 1, :usd => -1) # => mi/usd Unit.new(:mi => 1, :s => -1, Converter.converter(:uk)) * Unit.new(:s => 1, :usd => -1, Converter.converter(:us)) # => TypeError 1 / Unit.new(:mi => 1, :s => -1) # => s/mi
converter | [R] | |
units | [R] |
Creates a new (composite) Unit. It is passed a hash of the form {:unit => exponent}, and the Converter to use.
Examples:
Unit.new(:m => 1, :s => -1, Converter.converter(:uk)) # => m/s Unit.new(:mi => 1, :s => -2) # => mi/s**2
See also Converter, Converter.converter