From: Jeremy McAnally Date: 2007-01-08T14:50:41+09:00 Subject: Re: Grouping elements of an array? class Array def group_in(how_many) ret_array = [] new_array = [] self.each {|elem| how_many.times { new_array << self.shift }; ret_array << new_array; new_array = []; } ret_array end end x = [1,2,3,4,5,6,7,8,9] x = x.group_in(3) require 'pp' pp x >> [[1, 2, 3], [4, 5, 6], [7, 8, 9]] That should do it. :) Not sure if it's the best way, but it will work. --Jeremy On 1/8/07, Sam Kong wrote: > Hello, > > 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]] > > I want to transform the array into an array of arrays with fixed number > of elements. > I think there should be a method but I can't find it. > > I can iterate the elements using a counter to divide but it's so > boring. > > Thanks in advance. > > Sam > > > -- My free Ruby e-book: http://www.humblelittlerubybook.com/book/ My blogs: http://www.mrneighborly.com/ http://www.rubyinpractice.com/