Method: UnitMeasurements::NumericMethods.define_numeric_method_for

Defined in:
lib/unit_measurements/extras/numeric_methods.rb

.define_numeric_method_for(unit, unit_group) ⇒ Unit (private)

Defines a numeric method for a specific unit within a unit_group. The method is defined dynamically using define_method and associates the unit with the numeric value.

Parameters:

  • The unit (or its name) for which the numeric method needs to be defined.

  • The unit group to which the unit belongs.

Returns:

  • The unit instance for which the method was defined.

See Also:

Since:

  • 5.14.0



70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/unit_measurements/extras/numeric_methods.rb', line 70

def define_numeric_method_for(unit, unit_group)
  unit = unit.is_a?(Unit) ? unit : unit_group.unit_for!(unit)

  unit.names.each do |method_name|
    # Check if the name contains alphabetic characters
    next unless method_name =~ /^[a-zA-Z]+$/

    Numeric.define_method(method_name) do
      unit_group.new(self, unit)
    end
  end

  unit
end