252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
|
# File 'lib/lazy_enumerator.rb', line 252
def chunk(init_val = (init_given = true; nil))
cls = (self.kind_of? Enumerator) ? self.class : Enumerator
cls.new do |yielder|
chunk = nil
each do |elt|
new_val = init_given ? yield(elt, init_val) : yield(elt)
if chunk.nil?
chunk = [new_val, [elt]]
elsif new_val == init_val
chunk[1] << elt
else
yielder.yield chunk
chunk = [new_val, [elt]]
end
init_val = new_val
end
yielder.yield chunk if chunk
end
end
|