Module: Polyfill::V2_3::Enumerable::Instance::ChunkWhile::Method

Defined in:
lib/polyfill/v2_3/enumerable/instance/chunk_while.rb

Instance Method Summary collapse

Instance Method Details

#chunk_whileObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/polyfill/v2_3/enumerable/instance/chunk_while.rb', line 9

def chunk_while
  block = Proc.new

  return [self] if size == 1

  Enumerator.new do |yielder|
    output = []
    each_cons(2).with_index(1) do |(a, b), run|
      if run == size - 1
        if block.call(a, b)
          output.push(a, b)
          yielder << output
        else
          output.push(a)
          yielder << output
          yielder << [b]
        end
      else
        output.push(a)
        unless block.call(a, b)
          yielder << output
          output = []
        end
      end
    end
  end
end