Module: UnitMeasurements::Conversion

Included in:
Measurement
Defined in:
lib/unit_measurements/conversion.rb

Overview

The UnitMeasurements::Conversion mixin module defines methods for converting quantity of the measurement to various numeric types. These methods allow for flexibility in handling measurements in different numeric formats.

This module is included into the Measurement class to allow conversion of the measurement quantity to other numeric types.

See Also:

Author:

Since:

  • 1.7.0

Instance Method Summary collapse

Instance Method Details

#to_cMeasurement

Converts quantity of the measurement to Complex.

Examples:

UnitMeasurements::Length.new(2.25567, "km").to_c
=> 2.25567+0i km

Returns:

  • (Measurement)

    A new Measurement instance with the quantity converted to a Complex number.

Author:

Since:

  • 1.7.0



73
74
75
# File 'lib/unit_measurements/conversion.rb', line 73

def to_c
  self.class.new(quantity.to_c, unit)
end

#to_dMeasurement

Converts quantity of the measurement to BigDecimal.

Examples:

UnitMeasurements::Length.new(2.25567, "km").to_d
=> 2.25567 km

Returns:

  • (Measurement)

    A new Measurement instance with the quantity converted to a BigDecimal.

Author:

Since:

  • 1.7.0



88
89
90
# File 'lib/unit_measurements/conversion.rb', line 88

def to_d
  self.class.new(quantity.to_d, unit)
end

#to_fMeasurement

Converts quantity of the measurement to Float.

Examples:

UnitMeasurements::Length.new(2.25567, "km").to_f
=> 2.25567 km

Returns:

  • (Measurement)

    A new Measurement instance with the quantity converted to a Float.

Author:

Since:

  • 1.7.0



43
44
45
# File 'lib/unit_measurements/conversion.rb', line 43

def to_f
  self.class.new(quantity.to_f, unit)
end

#to_iMeasurement

Converts quantity of the measurement to Integer.

Examples:

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

Returns:

  • (Measurement)

    A new Measurement instance with the quantity converted to an Integer.

Author:

Since:

  • 1.7.0



28
29
30
# File 'lib/unit_measurements/conversion.rb', line 28

def to_i
  self.class.new(quantity.to_i, unit)
end

#to_rMeasurement

Converts quantity of the measurement to Rational.

Examples:

UnitMeasurements::Length.new(2.25567, "km").to_r
=> 225567/100000 km

Returns:

  • (Measurement)

    A new Measurement instance with the quantity converted to a Rational.

Author:

Since:

  • 1.7.0



58
59
60
# File 'lib/unit_measurements/conversion.rb', line 58

def to_r
  self.class.new(quantity.to_r, unit)
end