each_slice will slice an enumerable object to a specific size without repeating
[1,2,3,4].each_slice(2) do |chunk|
p chunk
end
# [1,2]
# [3,4]
This is in contrast to each_cons. It will slice the enumerable to a specific size but with repeats.
[1,2,3,4].each_cons(2) do |chunk|
p chunk
end
# [1,2]
# [2,3]
# [3,4]
[1,2,3,4].each_slice(2) do |chunk|
p chunk
end
# [1,2]
# [3,4]
This is in contrast to each_cons. It will slice the enumerable to a specific size but with repeats.
[1,2,3,4].each_cons(2) do |chunk|
p chunk
end
# [1,2]
# [2,3]
# [3,4]
Comments
Post a Comment