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

### Methods

* ** / == compatible_with? inspect method_missing new to_s unitless?

### Attributes

converter | [R] |
units | [R] |

### Public Class methods

new(units = {}, converter = Units::Converter.current)

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

### Public Instance methods

*(other)

Multiplies with the given Unit.

**(p)

Raises to the given power.

/(other)

Divides by the given Unit.

==(other)

Returns `true` iff the two Units are equals, i.e., iff they have the same exponent for all units, and they both use the same Converter.

compatible_with?(other)

Returns `true` iff this Unit is compatible with the given Unit. This is less strict than equality because for example hours are compatible with seconds ("we can add them"), but hours are not seconds.

inspect()

Alias for to_s

method_missing(m, *args, &blk)
to_s()

Returns a human readable string representation.

unitless?()

Returns `true` iff this Unit has all exponents equal to 0.

[Validate]