Method: Units.unit

Defined in:
lib/units/system.rb

.unit(unit_symbol) ⇒ Object

get unit definition

Raises:

  • (ArgumentError)


62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/units/system.rb', line 62

def self.unit(unit_symbol)
  ud = UNITS[unit_symbol]
  if ud.nil?
    factor = 1.0
    if factor_name = PREFIXES[unit_symbol]
      ud = UnitDefinition.new(nil, *factor_name)
    else
      u = unit_symbol.to_s
      PREFIXES.each_pair do |prefix, (f,name)|
        prefix = prefix.to_s
        if u[0...prefix.length] == prefix
          factor = f
          ud = UNITS[u[prefix.length..-1].to_sym]
          if ud
            ud = ud.dup
            ud.name = "#{name}#{ud.name}"
            break
          end
        end
      end
    end
    ud.factor *= factor if ud
    ud.decomposition *= factor if ud && ud.decomposition
  end
  raise ArgumentError,"Invalid Units #{unit_symbol}" unless ud
  ud
end