Module: Phys::UnitsNumericMixin

Included in:
Quantity
Defined in:
lib/phys/units/units_mixin.rb

Overview

ActiveSupport-like mix-in. Caution: This kind of global change will cause unexpected problems.

Examples:

require 'phys/units'

class Numeric
  include Phys::UnitsNumericMixin
end

1.miles/hr >> m/s     #=> Phys::Quantity[0.44704,"m/s"]
1.(km/s) >> miles/hr  #=> Phys::Quantity[(1/0.00044704),"miles/hr"]

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/phys/units/units_mixin.rb', line 81

def method_missing(method, *args, &block)
  if unit = Unit.find_unit(method)
    raise "argument must be empty" unless args.empty?
    case self
    when Integer
      Quantity.new(Rational(self), method, unit)
    when Numeric
      Quantity.new(self, method, unit)
    else
      self * Quantity.new(1, method, unit)
    end
  else
    method_missing_units_alias(method, *args, &block)
  end
end

Instance Method Details

#call(unit) ⇒ Object



97
98
99
# File 'lib/phys/units/units_mixin.rb', line 97

def call(unit)
  self * unit
end

#method_missing_units_aliasObject



79
# File 'lib/phys/units/units_mixin.rb', line 79

alias method_missing_units_alias method_missing