Units

This is a very extensive SI units system.

Usage

Here are some examples of usage.

  1.bit/s + 8.bytes/s
  (1.bit/s).to(byte/s)
  1.mile.to(feet)
  1.acre.to(yd**2)
  1.acre.to(sq_yd)
  1.gallon.to(self.L)
  1.lb.to(kg)
  1.m.s.to(m.s)
  1.sq_mi.to(km**2)
  1.mile.to(km)
  1.usd.to(twd)

Notes

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

Methods
Classes and Modules
Module Units::Config
Class Units::Converter
Class Units::Unit
Class Units::Value
Public Instance methods
method_missing(m, *args, &blk)
# File lib/facets/more/units.rb, line 57
  def method_missing(m, *args, &blk)
    if args.length == 1
      args[0] = Units::Converter.converter(args[0]) if not args[0].is_a? Units::Converter
      return Units::Unit.new({m => 1}, args[0]) if args[0].registered?(m)
    elsif Units::Converter.current.registered?(m)
      raise ArgumentError, "Wrong number of arguments" if args.length != 0
      return Units::Unit.new({m => 1}, Units::Converter.current)
    end
    super
  end
with_unit_converter(name) {|| ...}

Executes the block with the current Converter changed to the given Converter. This allows to temporarily change the Converter used by default.

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/facets/more/units.rb, line 83
  def with_unit_converter(name, &blk) # :yields:
    Units::Converter.with_converter(name, &blk)
  end