Top Level Namespace

Defined Under Namespace

Classes: Numeric, Unit

Instance Method Summary collapse

Instance Method Details

#Unit(*args) ⇒ Object

Raises:

  • (ArgumentError)


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/unit/constructor.rb', line 1

def Unit(*args)
  value = Numeric === args.first ? args.shift : 1
  value = Rational(value, args.shift) if Numeric === args.first

  system = args.index {|x| Unit::System === x }
  system = system ? args.delete_at(system) : Unit.default_system

  unit = args.index {|x| String === x }
  unit = system.parse_unit(args.delete_at(unit)) if unit

  unless unit
    unit = args.index {|x| Array === x }
    unit = args.delete_at(unit) if unit
  end

  unit ||= []
  system.validate_unit(unit)

  raise ArgumentError, 'wrong number of arguments' unless args.empty?

  Unit.new(value, unit, system)
end