Module: Polyfill::V2_6::Array

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

Instance Method Summary collapse

Instance Method Details

#difference(*arrays) ⇒ Object



4
5
6
7
8
# File 'lib/polyfill/v2_6/array.rb', line 4

def difference(*arrays)
  arrays.reduce([*self]) do |me, array|
    me - array
  end
end

#to_hObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/polyfill/v2_6/array.rb', line 10

def to_h
  return super unless block_given?

  block = ::Proc.new

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

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

    pair = pair.to_ary

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

    pair
  end

  pairs.to_h
end

#union(*arrays) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/polyfill/v2_6/array.rb', line 34

def union(*arrays)
  return self | [] if arrays.empty?

  arrays.reduce(self) do |me, array|
    me | array
  end
end