Module: UnitMeasurements::Arithmetic

Extended by:
Forwardable
Included in:
Measurement
Defined in:
lib/unit_measurements/arithmetic.rb

Overview

The UnitMeasurements::Arithmetic mixin module provides methods for performing arithmetic operations (addition, subtraction, multiplication, division, etc) on measurements of the same unit group. In case the measurements represents different units, the left hand side takes precedence while performing the arithmetic operation on them.

This module is included in the Measurement class to allow arithmetic operations on the measurements.

See Also:

Author:

Since:

  • 1.4.0

Instance Method Summary collapse

Instance Method Details

#*(other) ⇒ Measurement Also known as: scale, times, multiply

Multiplies the quantity of the current measurement by the quantity of the other measurement or a numeric value.

Examples:

UnitMeasurements::Length.new(2, "km") * UnitMeasurements::Length.new(3, "in")
=> 0.0001524 km

UnitMeasurements::Length.new(2, "km") * 2+2i
=> 4+2i km

Parameters:

  • other (Numeric|Measurement)

    The value to be multiplied. It can be a numeric value or another measurement.

Returns:

  • (Measurement)

    A new Measurement instance with the multiplied quantity.

Author:

Since:

  • 1.4.0



85
86
87
# File 'lib/unit_measurements/arithmetic.rb', line 85

def *(other)
  arithmetic_operation(other, :*)
end

#**(other) ⇒ Measurement Also known as: pow, ^

Raises the quantity of the current measurement to the power of the quantity of the other measurement or numeric value.

When other is an instance of Measurement, the quantity to raise is calculated by converting the other measurement to the unit of the current measurement, and then the quantity of the current measurement is raised to the converted quantity.

Examples:

UnitMeasurements::Length.new(2, "km") ** UnitMeasurements::Length.new(3, "m")
=> 1.00208160507963279 km

UnitMeasurements::Length.new(2, "km") ** 3
=> 8 km

UnitMeasurements::Length.new(8, "km") ** 1/3r
=> 2 km

Parameters:

  • other (Numeric|Measurement)

    The value to be raised. It can be a numeric value or another measurement.

Returns:

  • (Measurement)

    A new Measurement instance with the raised quantity.

Author:

Since:

  • 5.1.0



139
140
141
# File 'lib/unit_measurements/arithmetic.rb', line 139

def **(other)
  arithmetic_operation(other, :**)
end

#+(other) ⇒ Measurement Also known as: add

Adds the quantity of the other measurement or a numeric value to the quantity of the current measurement.

Examples:

UnitMeasurements::Length.new(1, "km") + UnitMeasurements::Length.new(1, "m")
=> 1.001 km

UnitMeasurements::Length.new(1, "km") + 4.5
=> 5.5 km

Parameters:

  • other (Numeric|Measurement)

    The value to be added. It can be a numeric value or another measurement.

Returns:

  • (Measurement)

    A new Measurement instance with the combined quantity.

Author:

Since:

  • 1.4.0



41
42
43
# File 'lib/unit_measurements/arithmetic.rb', line 41

def +(other)
  arithmetic_operation(other, :+)
end

#-(other) ⇒ Measurement Also known as: subtract

Subtracts the quantity of the other measurement or a numeric value from the quantity of the current measurement.

Examples:

UnitMeasurements::Length.new(1, "km") - UnitMeasurements::Length.new(2, "in")
=> 0.9999492 km

UnitMeasurements::Length.new(2, "km") - 1e+2
=> -98.0 km

Parameters:

  • other (Numeric|Measurement)

    The value to be subtracted. It can be a numeric value or another measurement.

Returns:

  • (Measurement)

    A new Measurement instance with the subtracted quantity.

Author:

Since:

  • 1.4.0



63
64
65
# File 'lib/unit_measurements/arithmetic.rb', line 63

def -(other)
  arithmetic_operation(other, :-)
end

#-@Measurement Also known as: inverse, negate

Negates the quantity of the measurement.

Examples:

-UnitMeasurements::Length.new(2, "km")
=> -2 km

-UnitMeasurements::Length.new(-2, "km")
=> 2 km

Returns:

  • (Measurement)

    A new Measurement instance with the negated quantity.

Author:

Since:

  • 5.1.0



158
159
160
# File 'lib/unit_measurements/arithmetic.rb', line 158

def -@
  self.class.new(-self.quantity, self.unit)
end

#/(other) ⇒ Measurement Also known as: divide

Divides the quantity of the current measurement by the quantity of the other measurement or a numeric value.

Examples:

UnitMeasurements::Length.new(4, "km") / UnitMeasurements::Length.new(2, "km")
=> 2 km

UnitMeasurements::Length.new(2, "km") / 2
=> 1 km

Parameters:

  • other (Numeric|Measurement)

    The value to be divided. It can be a numeric value or another measurement.

Returns:

  • (Measurement)

    A new Measurement instance with the divided quantity.

Author:

Since:

  • 1.4.0



109
110
111
# File 'lib/unit_measurements/arithmetic.rb', line 109

def /(other)
  arithmetic_operation(other, :/)
end

#arithmetic_operation(other, operator) ⇒ Measurement (private)

Performs an arithmetic operation (addition, subtraction, multiplication, or division) on the current measurement and another numeric value.

Parameters:

  • other (Numeric|Measurement)

    The value to be used in the arithmetic operation. It can be a numeric value or another measurement.

  • operator (Symbol)

    The operator to be used for the operation.

Returns:

  • (Measurement)

    A new Measurement instance with the result of the arithmetic operation.

Author:

Since:

  • 1.4.0



219
220
221
222
223
# File 'lib/unit_measurements/arithmetic.rb', line 219

def arithmetic_operation(other, operator)
  other, _ = coerce(other)

  self.class.new(self.quantity.public_send(operator, other.convert_to(self.unit).quantity), self.unit)
end

#coerce(other) ⇒ Array<Measurement> (private)

Coerces a numeric value or another measurement for arithmetic operations.

Parameters:

  • other (Numeric|Measurement)

    The value to be coerced. It can be a numeric value or another measurement.

Returns:

  • (Array<Measurement>)

    An array containing the coerced values.

Raises:

  • (TypeError)

    If the coercion is not possible due to incompatible types.

Author:

Since:

  • 1.4.0



197
198
199
200
201
202
203
# File 'lib/unit_measurements/arithmetic.rb', line 197

def coerce(other)
  case other
  when Numeric    then [self.class.new(other, self.unit), self]
  when self.class then [other, self]
  else                 raise TypeError, "Cannot coerce #{other.class} to #{self.class}"
  end
end

#nonzero?TrueClass|FalseClass

Checks whether the quantity of the measurement is nonzero.

Examples:

UnitMeasurements::Length.new(2, "km").nonzero?
=> true

UnitMeasurements::Length.new(0, "km").nonzero?
=> false

Returns:

  • (TrueClass|FalseClass)

    true if the quantity is nonzero otherwise it returns false.

Author:

Since:

  • 5.1.0



178
179
180
# File 'lib/unit_measurements/arithmetic.rb', line 178

def nonzero?
  quantity.nonzero? ? true : false
end