Module: Measurb::CoreExt

Defined in:
lib/measurb/core_ext.rb

Overview

Module for extending core classes

Class Method Summary collapse

Class Method Details

.add_numeric_dimension(name, dimension_class_name, abbrev) ⇒ nil

Add the dimension name method to ‘Numeric`

Examples:

42.respond_to?(:inches) #=> false

Measurb::CoreExt.add_numeric_dimension(:inches, 'Inches', 'in')

42.respond_to?(:inches) #=> true

Parameters:

  • name (Symbol, String)

    Name of the dimension

  • dimension_class_name (String)

    Name of the dimension class

  • abbrev (String)

    Abbreviation for the dimension name

Returns:

  • (nil)


19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/measurb/core_ext.rb', line 19

def self.add_numeric_dimension(name, dimension_class_name, abbrev)
  def_string = <<-EOS
    def #{name}(precision = #{DEFAULT_PRECISION})
      Measurb::#{dimension_class_name}.new(self, precision)
    end
  EOS

  unless abbrev.nil?
    def_string << "\nalias_method :#{abbrev}, :#{name}"
  end

  Numeric.class_eval(def_string)
end