Class Units::Value
In: lib/facets/more/units.rb
Parent: Numeric

This class represents a Value with a numeric value and a Unit. The numeric value can be any Numeric, though it is not recommended to use Values.

A Value can be added to, subtracted from and multiplied with another value, though only when both Values are using the same Converter. While multiplication is always possible, adding or subtracting values with incompatible Units results in a TypeError. When two Units are compatible but not the same, the Value with the larger of the Units is converted to the smaller of the Units. For example adding 100 seconds and 1 minute, the latter is converted to 60 seconds because a second is smaller than a minute. The result is 160 seconds.

Methods

converter   inspect   method_missing   new   to   to_s  

Attributes

unit  [R]  The Unit of this value.
value  [R]  The numeric value of this Value.

Public Class methods

Creates a new Value with the given numeric value and the given unit. Simply returns the given value if the given unit is valueless, i.e., when unit.unitless? is true.

Public Instance methods

Returns the Converter used by this Value‘s Unit. Equivalent to unit.converter.

inspect()

Alias for to_s

Converts this Value to the given Unit. This only works if the Converters used by this Value‘s Unit and the given Unit are the same. It obviously fails if the Units are not compatible (can‘t add apples and oranges).

Returns a human readable string representation.

[Validate]