Class: Numeric

Inherits:
Object
  • Object
show all
Defined in:
lib/unitwise/ext/numeric.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &block) ⇒ Object

Converts numeric to a measurement by the method name



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/unitwise/ext/numeric.rb', line 17

def method_missing(meth, *args, &block)
  if args.empty? && !block_given?
    unit = (match = /\Ato_(\w+)\Z/.match(meth.to_s)) ? match[1] : meth
    begin
      convert_to(unit)
    rescue Unitwise::ExpressionError
      super(meth, *args, &block)
    end
  else
    super(meth, *args, &block)
  end
end

Instance Method Details

#convert_to(unit) ⇒ Unitwise::Measurement

Converts numeric to a measurement

Examples:

26.2.convert_to('mile') # => #<Unitwise::Measurement 1 mile>

Parameters:

  • unit (Unitwise::Unit, String)

    The unit to use in the measurement

Returns:



8
9
10
# File 'lib/unitwise/ext/numeric.rb', line 8

def convert_to(unit)
  Unitwise::Measurement.new(self, unit)
end