Class | Eymiha::NumericWithUnits |
In: |
lib/eymiha/units/numeric_with_units.rb
|
Parent: | Object |
A NumericWithUnits is the intersection of a Numeric and a UnitsHash.
Unit-sensitive coding is made much easier using an object that transparently adds units to a common everyday Numerics. Everything that can be done to a Numeric is still there, but explicit and implicit unit conversions are present in all of the operations.
With this, numbers with units can be created easily. For example,
2.feet # a length of 2 feet 5.inches^2 # an area of 5 square inches 44.5.ft/sec # a velocity of 44.5 feet per second
This should provide a good starting point for using the Units framework. Also pay attention to the examples given in the method documentation; some of the dynamic features of the framework are exposed in them.
kind_of? | -> | old_kind_of? |
Returns the type of exponentiation merging during alignment.
Returns a new NumericWithUnits instance whose numeric part is set to numeric and whose units part is set to a units hash for the unit raised to the provided power.
Returns a new NumericWithUnits containing the numeric part of the instance modulo the numeric part of the value, with the units part equal to the instance‘s units part. If the value is a Numeric, the units are assumed to match. If the UnitsMeasures don‘t match, a UnitsException is raised.
30.in % 2.ft # 0.5 feet 5.in % 2.3 # 0.4 inches 5.in % 2.sec # UnitsException
Returns a new NumericWithUnits containing the product of the instance and the value. The units of the two factors are merged. If the value is not a Numeric nor NumericWithUnits, a UnitsException is thrown.
6.in * 2.ft # 1 foot^2 6.in * 2 # 12 inches 6.in * 2.sec # 12 ft sec 6.in * "hello" # UnitsException
Returns a new NumericWithUnits containing the numeric and units parts of the instance both raised to the valueth power. If the value is not a Numeric, a UnitsException is thrown.
6.in ** 3 # 216 in^3 6.in ** 3.in # UnitsException
Returns a new NumericWithUnits containing the sum of the instance and the value. If the value is a Numeric, the units are assumed to match. If the UnitsMeasures don‘t match, a UnitsException is raised.
6.inches + 1.foot # 18 inches 6.inches + 1 # 7 inches 6.inches + 1.sec # UnitsException
Returns a new NumericWithUnits containing the difference between the instance and the value. If the value is a Numeric, the units are assumed to match. If the UnitsMeasures don‘t match, a UnitsException is raised.
6.inches - 1.foot # -6 inches 6.inches - 1 # 5 inches 6.inches - 1.sec # UnitsException
Returns a new NumericWithUnits containing the value of the instance divided by the value. The units of the result are the units of the instance merged with the recipricol of the units of the value. If the value is neither a Numeric nor NumericWithUnits, a UnitsException is thrown.
6.in / 2.ft # 0.25 6.in / 2 # 3 inches 6.in / 2.sec # 3 ft / sec 6.in / "hello" # UnitsException
Compares the numeric and units parts of the instance with the value. If the value is a Numeric, the units are assumed to match. If the UnitsMeasures don‘t match, a UnitsException is raised.
2.ft <=> 1.yd # -1 3.ft <=> 1.yd # 0 4.ft <=> 1.yd # 1 4.ft <=> 3.5 # 1 4.ft <=> 2.minutes # UnitsException
Returns a new NumericWithUnits containing the numeric and units parts of the instance with just the units raised to the valueth power. If the value is not a Numeric, a UnitsException is thrown.
6.in ^ 3 # 6 in^3 6.in ^ 3.in # UnitsException
Returns a new NumericWithUnits whose value is equivalent to that of the instance, but whose units are aligned to the target, according to the value of derived_align_type. If all is true, then the UnitsMeasure of the instance and the target must match exactly, or else a UnitsException is raised.
(80.miles_per_hour).align(1.min,false) # 1.3333333 mi / min
Returns true if the value and the instance are within a distance epsilon of each other. If the value is a Numeric, the units are assumed to match. If the UnitsMeasures don‘t match, a UnitsException is raised.
1.ft.approximately_equals? 0.33.yd # false 1.ft.approximately_equals? 0.333333.yd # true
Returns a copy of the instance converted to the target_units. Note that the conversion is only with respect to the UnitsMeasures of the target_units - the remainder of the units will remain unconverted.
Returns a new NumericWithUnits whose value is extended by raising it to the power, or by multiplying it by units raised to the power.
7.miles.extend(nil,2) # 49 mi^2 5.feet.extend(10.ft,2) # 500 ft^3
Returns a String formatted using the named format defined in the instance‘s measure. Raises a UnitsException if either the unit part of the instance has no defined UnitsMeasure or a format with the given name does not exist in that UnitsMeasure.
Returns a new NumericWithUnits containing the numeric part of the value modulo the numeric part of the instance, with the units part equal to the instance‘s units part. If the value is a Numeric, the units are assumed to match. If the UnitsMeasures don‘t match, a UnitsException is raised.
30.in % 2.ft # 24 inches 5.in % 2.3 # 2.3 inches 5.in % 2.sec # UnitsException
Returns true if the instance or it‘s numeric part is a kind of klass.
28.feet.kind_of? String # false 28.feet.kind_of? NumericWithUnits # true 28.feet.kind_of? Numeric # true 28.feet.kind_of? Integer # true 28.feet.kind_of? Float # false 28.0.feet.kind_of? Float # true
Note while NumericWithUnits actually descends from Object, it acts as if it is inherited from the class of the numeric part of the instance, since it forwards any unknown method calls to it. In this way the duck really is a duck.
Return a new NumericWithUnits that is equivalent to the instance but whose unit contains no derived UnitMeasures.
Returns a String representation of the instance, using the named format if provided.
15.5.minutes.to_s # 15.5 minutes 15.5.minutes.to_seconds.to_s # 930.0 seconds 15.5.minutes.in_seconds.to_s # 930.0 seconds 15.5.minutes.seconds.to_s # 930.0 seconds (15.5.minutes+1).seconds.to_s # 990.0 seconds (15.5.minutes.seconds+1).to_s # 931.0 seconds 10.feet_per_minute.to_s # 10 ft / min seconds_per_hour.to_s # 3600.0 14.5.inches.to_s(:feet_inches_and_32s) # "1 foot 2-16/32 inches"
Returns a new NumericWithUnits whose numeric part is the target of the Numeric‘s unite method.
28.ft^2.unite("seconds") # 28 seconds