I have an array that is something like this:
arr = [4, 5, 6, 7, 8, 4, 45, 11]
I want a fancy method like
sub_arrays = split (arr, 3)
This should return the following: [[4, 5, 6], [7,8,4], [45,11]]
Note: This question is not a duplicate of "How to chunk an array". The chunk question is asking about processing in batches and this question is about splitting arrays.
each_slicereturns an Enumerable, so if that’s enough for you, you don’t need to callto_a.In 1.8.6 you need to do:
If you just need to iterate, you can simply do: