Sha256: 9f494462574d4f8f5ea1a058d425001ae001970f29cbc423947e955550a352ca

Contents?: true

Size: 1.63 KB

Versions: 1

Compression:

Stored size: 1.63 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.6.0 lib/polyfill/v2_3/enumerable/instance/chunk_while.rb