Method: Array#each_slice

Defined in:
lib/nano/enumerable/each_slice.rb

#each_slice(n = nil, &yld) ⇒ Object

Iterates over n elements at a time. If n is not given then the arity of the block determines the slicing quantity.

[1, 2, 3, 4].each_slice(2){ |a,b| ... }

This is a specialized version Enumerable#each_slice but optimized specifically for Array.



48
49
50
51
52
53
54
55
# File 'lib/nano/enumerable/each_slice.rb', line 48

def each_slice(n=nil, &yld)
  n = yld.arity.abs unless n
  i=0
  while i < self.length
    yld.call(*self.slice(i,n))
    i+=n
  end
end