Module: UnitMeasurements::Comparison

Includes:
Comparable
Included in:
Measurement
Defined in:
lib/unit_measurements/comparison.rb

Overview

The UnitMeasurements::Comparison mixin module is included in measurement classes to enable comparison operations (e.g., less than, equal to, greater than, etc.) between two measurements of the same unit group.

This module is included in the Measurement class to allow comparison of two measurements.

See Also:

Author:

Since:

  • 1.3.0

Instance Method Summary collapse

Instance Method Details

#<=>(object) ⇒ Object

This method is used to compare the quantity of two measurements. It implements the comparison logic based on the <=> method defined in the Comparable module.

Examples:

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

UnitMeasurements::Length.parse("1 km") == UnitMeasurements::Length.parse("1000 m")
=> true

UnitMeasurements::Length.parse("1 km") != UnitMeasurements::Length.parse("1 m")
=> true

UnitMeasurements::Length.parse("1 km") < UnitMeasurements::Length.parse("0.5 km")
=> false

UnitMeasurements::Length.parse("1 km") > UnitMeasurements::Length.parse("0.5 km")
=> true

UnitMeasurements::Length.parse("1 km") <= UnitMeasurements::Length.parse("0.5 km")
=> false

UnitMeasurements::Length.parse("1 km") >= UnitMeasurements::Length.parse("0.5 km")
=> true

UnitMeasurements::Length.new(1, "ft").between?(UnitMeasurements::Length.new(12, "in"), UnitMeasurements::Length.new(24, "in"))
=> true

Parameters:

  • object (Measurement)

    The measurement instance to compare with.

Returns:

  • nil if the comparison is not possible (e.g., if the object is not of the same unit group). A negative integer if self is less than object. Zero if self is equal to object. A positive integer if self is greater than object.

Author:

Since:

  • 1.3.0



58
59
60
61
62
# File 'lib/unit_measurements/comparison.rb', line 58

def <=>(object)
  return nil unless object.is_a?(self.class)

  quantity <=> object.convert_to(unit.name).quantity
end