Building an array which contains arrays grouped by 3 items (in a line)

Hello again. I have an array which contains several items (Array_a) and what I need is create a new array (Array_b) which includes same items of Array_a, but grouped in arrays within 3 items each.

For example:

Array_a: ['a1','a2','a3','b1','b2','b3','c1','c2','c3','d1','d2','d3']

After iteration:

Array_b: [ ['a1','a2','a3'],['b1','b2','b3'],['c1','c2','c3'], ['d1','d2','d3'] ]

Can anybody give me an idea of what iteration can I use to get that? If it could be done in one single line, much better. Thanks a lot.

Best!

Paco Reyes Interaction Designer www.xentido.com

paco.onrails wrote:

Hello again. I have an array which contains several items (Array_a) and what I need is create a new array (Array_b) which includes same items of Array_a, but grouped in arrays within 3 items each.

For example:

Array_a: ['a1','a2','a3','b1','b2','b3','c1','c2','c3','d1','d2','d3']

After iteration:

Array_b: [ ['a1','a2','a3'],['b1','b2','b3'],['c1','c2','c3'], ['d1','d2','d3'] ]

Can anybody give me an idea of what iteration can I use to get that? If it could be done in one single line, much better. Thanks a lot.

Best!

Paco Reyes Interaction Designer www.xentido.com

Hi Paco,

Take a look at in_groups_of.

http://www.railsbrain.com/api/rails-2.0.2/doc/index.html?a=M001093&name=in_groups_of

Peace, Phillip