Sha256: c3ffda7b6452f0ab31eacf5e8fef55dda45ddab3b8b4f47099c12415d24d611e

Contents?: true

Size: 1.62 KB

Versions: 1

Compression:

Stored size: 1.62 KB

Contents

require 'stringio'

module Polyfill
  module V2_3
    module Enumerable
      module Instance
        module ChunkWhile
          module Method
            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
          end

          refine ::Array do
            include Method
          end
          refine ::Dir do
            include Method
          end
          refine ::Enumerator do
            include Method
          end
          refine ::Hash do
            include Method
          end
          refine ::IO do
            include Method
          end
          refine ::Range do
            include Method
          end
          refine ::StringIO do
            include Method
          end
          refine ::Struct do
            include Method
          end

          def self.included(base)
            base.include Method
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
polyfill-0.5.0 lib/polyfill/v2_3/enumerable/instance/chunk_while.rb