Class: EnumerableFu::Zipper

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/enumerable_fu/zipping.rb

Instance Method Summary collapse

Methods included from Enumerable

#collecting, #rejecting, #selecting, #uniqing, #uniqing_by

Constructor Details

#initialize(enumerables) ⇒ Zipper

Returns a new instance of Zipper.


7
8
9
# File 'lib/enumerable_fu/zipping.rb', line 7

def initialize(enumerables)
  @enumerables = enumerables
end

Instance Method Details

#eachObject


11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/enumerable_fu/zipping.rb', line 11

def each
  enumerators = @enumerables.map(&:to_enum)
  while true
    chunk = enumerators.map do |enumerator|
      begin 
        enumerator.next
      rescue StopIteration
        nil
      end
    end
    break if chunk.all?(&:nil?)
    yield chunk
  end
end