Module: Polyfill::V2_6::Enumerable

Defined in:
lib/polyfill/v2_6/enumerable.rb

Instance Method Summary collapse

Instance Method Details

#to_hObject



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/polyfill/v2_6/enumerable.rb', line 4

def to_h
  return super unless block_given?

  block = ::Proc.new

  pairs = map do |elem|
    pair = block.call(elem)

    unless pair.respond_to?(:to_ary)
      raise TypeError, "wrong element type #{pair.class} (expected array)"
    end

    pair = pair.to_ary

    unless pair.length == 2
      raise ArgumentError, "element has wrong array length (expected 2, was #{pair.length})"
    end

    pair
  end

  pairs.to_h
end