Module: UnitConversions

Included in:
Numeric
Defined in:
lib/simplificator-conversions.rb

Constant Summary collapse

CONVERSIONS =
{
  'kg' => {
    't' => lambda() {|kg| kg / 1000.0 },
    'g' => lambda() {|kg| kg * 1000 },
  },
  'g' => {
    'kg' => lambda() {|g| g / 1000.0},
    't' => lambda() {|g| g / (1000.0 * 1000) },
  },
  't' => {
    'kg' => lambda() {|t| t * 1000},
    'g' => lambda() {|t| t * (1000 * 1000)},
  },
  'm2' => {
    'mm2' => lambda() {|m2| m2 * (1000 * 1000.0)},
  },
  'mm2' => {
    'm2' => lambda() {|mm2| mm2 / (1000 * 1000.0)},
  },
}
UNITS =
CONVERSIONS.keys
CONVERT_TO_PATTERN =
/(#{UNITS.join('|')})_to_(#{UNITS.join('|')})/

Instance Method Summary collapse

Instance Method Details

#method_missing_with_unit_conversions(m, *args) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/simplificator-conversions.rb', line 26

def method_missing_with_unit_conversions(m, *args)
  if args.length == 0
    m.to_s =~ CONVERT_TO_PATTERN    
    return CONVERSIONS[$1][$2].call(self) if CONVERSIONS[$1] && CONVERSIONS[$1][$2]
  end
  method_missing_without_unit_conversions(m, args)
end