Module: UnitMeasurements::Arithmetic
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.
Instance Method Summary collapse
-
#*(other) ⇒ Measurement
(also: #scale)
Multiplies the quantity of the current measurement by the quantity of the other measurement or a numeric value.
-
#**(other) ⇒ Measurement
Raises the quantity of the current measurement to the power of the quantity of the other measurement or numeric value.
-
#+(other) ⇒ Measurement
Adds the quantity of the other measurement or a numeric value to the quantity of the current measurement.
-
#-(other) ⇒ Measurement
Subtracts the quantity of the other measurement or a numeric value from the quantity of the current measurement.
-
#-@ ⇒ Measurement
Negates the quantity of the measurement.
-
#/(other) ⇒ Measurement
Divides the quantity of the current measurement by the quantity of the other measurement or a numeric value.
-
#nonzero? ⇒ TrueClass|FalseClass
Checks whether the quantity of the measurement is nonzero.
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.
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.
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.
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.
62 63 64 |
# File 'lib/unit_measurements/arithmetic.rb', line 62 def -(other) arithmetic_operation(other, :-) end |
#-@ ⇒ Measurement
Negates the quantity of the measurement.
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.
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.
169 170 171 |
# File 'lib/unit_measurements/arithmetic.rb', line 169 def nonzero? quantity.nonzero? ? true : false end |