Module: UnitMeasurements::NumericMethods
- Included in:
- Measurement
- Defined in:
- lib/unit_measurements/extras/numeric_methods.rb
Overview
This module provides methods to define Numeric
methods for a list of units within a unit group. If units are empty, it defaults to defining methods for all units in the unit group.
This module is included in the Measurement
class to allow defining numeric methods for specified units.
Class Method Summary collapse
-
.define_numeric_method_for(unit, unit_group) ⇒ Unit
private
Defines a numeric method for a specific
unit
within aunit_group
. -
.define_numeric_methods(*units) ⇒ Array<Unit>
Defines
Numeric
methods for specifiedunits
within the unit group.
Class Method Details
.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.
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 |
.define_numeric_methods(*units) ⇒ Array<Unit>
This method defines a numeric methods specifically for units that contain alphabetic characters in their names.
Defines Numeric
methods for specified units
within the unit group. If units
are empty, it defaults to defining methods for all units within the unit group.
43 44 45 46 47 48 49 50 |
# File 'lib/unit_measurements/extras/numeric_methods.rb', line 43 def define_numeric_methods(*units) unit_group = self units = units.empty? ? unit_group.units : units units.inject([]) do |units, unit| units << define_numeric_method_for(unit, unit_group) end end |