Skip to content

Commit

Permalink
+ Enumerable#chunk_while (Ruby 2.3)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcandre committed Apr 26, 2017
1 parent 1e5a817 commit ae51f76
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.rdoc
Expand Up @@ -79,6 +79,7 @@ Compatible with Ruby itself, JRuby and Rubinius.
* +bsearch_index+
* +dig+
* Enumerable
* +chunk_while+
* +grep_v+
* Hash
* +dig+
Expand Down
27 changes: 27 additions & 0 deletions lib/backports/2.3.0/enumerable/chunk_while.rb
@@ -0,0 +1,27 @@
unless Enumerable.method_defined? :chunk_while
require 'backports/tools/arguments'
require 'backports/1.9.1/enumerator/new'

module Enumerable
def chunk_while(&block)
raise ArgumentError, 'tried to create Proc object without a block' unless block
enum = self
Enumerator.new do |y|
acc = []
prev = Backports::Undefined
enum.each do |*elem|
elem = elem.first if elem.length == 1
unless prev == Backports::Undefined
unless block.call(prev, elem)
y.yield acc
acc = []
end
end
acc << elem
prev = elem
end
y.yield acc unless acc.empty?
end
end
end
end

0 comments on commit ae51f76

Please sign in to comment.