Skip to content

End of Life Ruby 3.0 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →

instance method

Enumerable#each_slice

enum.each_slice(n) { ... } → nil
enum.each_slice(n) → an_enumerator

Iterates the given block for each slice of <n> elements. If no block is given, returns an enumerator.

Example
(1..10).each_slice(3) { |a| p a }
# outputs below
[1, 2, 3]
[4, 5, 6]
[7, 8, 9]
[10]