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, #times, #multiply)
Multiplies the quantity of the current measurement by the quantity of the other measurement or a numeric value.
-
#**(other) ⇒ Measurement
(also: #pow, #^)
Raises the quantity of the current measurement to the power of the quantity of the other measurement or numeric value.
-
#+(other) ⇒ Measurement
(also: #add)
Adds the quantity of the other measurement or a numeric value to the quantity of the current measurement.
-
#-(other) ⇒ Measurement
(also: #subtract)
Subtracts the quantity of the other measurement or a numeric value from the quantity of the current measurement.
-
#-@ ⇒ Measurement
(also: #inverse, #negate)
Negates the quantity of the measurement.
-
#/(other) ⇒ Measurement
(also: #divide)
Divides the quantity of the current measurement by the quantity of the other measurement or a numeric value.
-
#arithmetic_operation(other, operator) ⇒ Measurement
private
Performs an arithmetic operation (addition, subtraction, multiplication, or division) on the current measurement and another numeric value.
-
#coerce(other) ⇒ Array<Measurement>
private
Coerces a numeric value or another measurement for arithmetic operations.
-
#nonzero? ⇒ TrueClass|FalseClass
Checks whether the quantity of the measurement is nonzero.
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.
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.
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.
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.
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.
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.
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.
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.
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.
178 179 180 |
# File 'lib/unit_measurements/arithmetic.rb', line 178 def nonzero? quantity.nonzero? ? true : false end |