I want to define a method that performs the following operation:
Given an array: array = [a, b, c, d, e, f, g, h, i]
My imaginary method (let’s call it nest_by) would produce these
results
array.nest_by(2)
=> [[a, b], [c, d], [d, e], [f, g], [h, i]]
array.nest_by(3)
=> [[a, b, c], [d, e, f], [g, h, i]]
array.nest_by(4)
=> [[a, b, c, d], [e, f, g, h], [i, nil, nil, nil]]
The contents of the array can be anything (including array’s
themselves).
This is sort of like the rails Array.in_groups_of(n), but uses the
array.size to determine the number of elements in each new nested
array.
Any thoughts?
Daniel