Joel VanderWerf
1/8/2007 6:03:00 AM
Farrel Lifson wrote:
> On 08/01/07, Sam Kong <sam.s.kong@gmail.com> wrote:
>> What's the best way to do the following?
>>
>> [1,2,3,4,5,6,7,8,9] => [[1,2,3],[4,5,6],[7,8,9]]
...
> [1,2,3,4,5,6,7,8,9].enum_slice(3).inject([]){|array,slice| array << slice}
Alternately,
[1,2,3,4,5,6,7,8,9].enum_for(:each_slice,3).to_a
=> [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407