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

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



83
84
85
# File 'lib/unit_measurements/arithmetic.rb', line 83

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

#**(other) ⇒ Measurement

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



134
135
136
# File 'lib/unit_measurements/arithmetic.rb', line 134

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

#+(other) ⇒ Measurement

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

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



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

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

#-@Measurement

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



151
152
153
# File 'lib/unit_measurements/arithmetic.rb', line 151

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

#/(other) ⇒ Measurement

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



105
106
107
# File 'lib/unit_measurements/arithmetic.rb', line 105

def /(other)
  arithmetic_operation(other, :/)
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



169
170
171
# File 'lib/unit_measurements/arithmetic.rb', line 169

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