Module: Enumerable

Defined in:
lib/taxjar/base.rb

Instance Method Summary collapse

Instance Method Details

#to_h(*arg) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/taxjar/base.rb', line 8

def to_h(*arg)
  h = {}
  each_with_index(*arg) do |elem, i|
    unless elem.respond_to?(:to_ary)
      raise TypeError, "wrong element type #{elem.class} at #{i} (expected array)"
    end

    ary = elem.to_ary
    if ary.size != 2
      raise ArgumentError, "wrong array length at #{i} (expected 2, was #{ary.size})"
    end

    h[ary[0]] = ary[1]
  end
  h
end