Van::Units

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).

Public Class Methods

append_features(m) click to toggle source
     # File lib/van/units/base.rb, line 99
 99:   def self.append_features(m)
100:     m.send(:extend, Units)
101:     super
102:   end
with_unit_converter(name) click to toggle source

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.

     # File lib/van/units/base.rb, line 120
120:   def with_unit_converter(name, &blk) # :yields:
121:     Units::Converter.with_converter(name, &blk)
122:   end

Public Instance Methods

const_missing(c) click to toggle source
    # File lib/van/units/base.rb, line 89
89:   def const_missing(c)
90:     if (Units::Converter.current.registered?(c) rescue false)
91:       return Units::Unit.new({c => 1}, Units::Converter.current)
92:     else
93:       ::Exception.with_clean_backtrace("const_missing") {
94:         super
95:       }
96:     end
97:   end
method_missing(m, *args, &blk) click to toggle source
    # File lib/van/units/base.rb, line 76
76:   def method_missing(m, *args, &blk)
77:     if args.length == 1
78:       args[0] = (Units::Converter.converter(args[0]) rescue nil) if not args[0].is_a? Units::Converter
79:       return Units::Unit.new({m => 1}, args[0]) if args[0] && args[0].registered?(m)
80:     elsif (Units::Converter.current.registered?(m) rescue false)
81:       raise ::ArgumentError, "Wrong number of arguments" if args.length != 0
82:       return Units::Unit.new({m => 1}, Units::Converter.current)
83:     end
84:     ::Exception.with_clean_backtrace("method_missing") {
85:       super
86:     }
87:   end

Private Instance Methods

with_unit_converter(name) click to toggle source

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.

     # File lib/van/units/base.rb, line 120
120:   def with_unit_converter(name, &blk) # :yields:
121:     Units::Converter.with_converter(name, &blk)
122:   end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.