From: Pit Capitain Date: 2006-11-07T23:46:11+09:00 Subject: Re: dividing an array Farrel Lifson schrieb: > On 07/11/06, Pit Capitain wrote: >> Farrel Lifson schrieb: >> > On 07/11/06, Brad Tilley wrote: >> >> Say I have an array that contains 144 strings. I want to divide the >> >> array into >> >> 12 sub-arrays each containing 12 strings. What is the Ruby Way of >> >> doing this? >> >> I've looked at slice array[0..11] etc, but that seems a bit clunky. >> > >> > require 'enumerator' >> > numbers = Array(0..143) >> > numbers.enum_slice(12).inject([]) do |array,slice| >> > array << slice >> > end >> >> require "enumerator" >> numbers = Array(0..143) >> numbers.enum_slice(12).to_a > > That's pretty cool Farrel, it was you who found the important method Enumerable#enum_slice. I just added the standard Enumerable#to_a method. Plus, I'm sure Robert K. is happy to see more and more people adding #inject to their toolbox :-) Regards, Pit