Module: Stick::Units

Included in:
Constants::CGS, Constants::MKS
Defined in:
lib/stick/units/base.rb,
lib/stick/units.rb,
lib/stick/currency.rb,
lib/stick/units/units.rb,
lib/stick/units/loaders.rb,
lib/stick/units/currency.rb

Overview

The namespace for all unit related classes. Mixing this in has the additional effect of making Units.with_unit_converter available without the Units. prefix, as well as the shortcuts for creating Units (see Units#method_missing).

http://en.wikipedia.org/wiki/Celsius_scale

degrees_Celsius vs Celsius_degrees
Kelvin

Kelvin  Celsius   Fahrenheit  Rankine   Delisle   Newton  Réaumur   Rømer

K       C         F           R         D         N       R         R

http://en.wikipedia.org/wiki/Conversion_of_units

++

Defined Under Namespace

Modules: Config Classes: BaseUnit, BinaryLoader, Converter, CurrencyLoader, IECBinaryLoader, LengthLoader, Loader, Regexps, SILoader, StandardLoader, Unit, Value

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args, &blk) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/stick/units/base.rb', line 76

def method_missing(m, *args, &blk)
  if args.length == 1
    args[0] = (Units::Converter.converter(args[0]) rescue nil) if not args[0].is_a? Units::Converter
    return Units::Unit.new({m => 1}, args[0]) if args[0] && args[0].registered?(m)
  elsif (Units::Converter.current.registered?(m) rescue false)
    raise ::ArgumentError, "Wrong number of arguments" if args.length != 0
    return Units::Unit.new({m => 1}, Units::Converter.current)
  end
  ::Exception.with_clean_backtrace("method_missing") {
    super
  }
end

Class Method Details

.append_features(m) ⇒ Object



99
100
101
102
# File 'lib/stick/units/base.rb', line 99

def self.append_features(m)
  m.send(:extend, Units)
  super
end

.with_unit_converter(name, &blk) ⇒ Object

Executes the block with the current Converter changed to the given Converter. This allows to temporarily change the Converter used by default. A Converter object can be given, or the name of a registered Converter.

Example:

with_unit_converter(:uk) {
  puts 1.cwt.to(lb) # => 112.0 lb
}

with_unit_converter(:us) {
  puts 1.cwt.to(lb) # => 100.0 lb
}

See also Converter.current.



120
121
122
# File 'lib/stick/units/base.rb', line 120

def with_unit_converter(name, &blk) # :yields:
  Units::Converter.with_converter(name, &blk)
end

Instance Method Details

#const_missing(c) ⇒ Object



89
90
91
92
93
94
95
96
97
# File 'lib/stick/units/base.rb', line 89

def const_missing(c)
  if (Units::Converter.current.registered?(c) rescue false)
    return Units::Unit.new({c => 1}, Units::Converter.current)
  else
    ::Exception.with_clean_backtrace("const_missing") {
      super
    }
  end
end