# Tricky ruby array grouping method

**URL:** <https://discuss.rubyonrails.org/t/tricky-ruby-array-grouping-method/15636>\
**Category:** rubyonrails-talk\
**Created:** [July 13, 2007, 8:52pm UTC](https://discuss.rubyonrails.org/t/tricky-ruby-array-grouping-method/15636 "2007-07-13T20:52:28Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Daniel\_Lunde](https://avatars.discourse-cdn.com/v4/letter/d/c89c15/32.png) [@Daniel\_Lunde](https://discuss.rubyonrails.org/u/Daniel_Lunde)\
**Post date:** [July 13, 2007, 8:52pm UTC](https://discuss.rubyonrails.org/t/tricky-ruby-array-grouping-method/15636/1 "2007-07-13T20:52:28Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Daniel\_Lunde](https://avatars.discourse-cdn.com/v4/letter/d/c89c15/32.png) [@Daniel\_Lunde](https://discuss.rubyonrails.org/u/Daniel_Lunde)\
**Post date:** [July 13, 2007, 9:59pm UTC](https://discuss.rubyonrails.org/t/tricky-ruby-array-grouping-method/15636/2 "2007-07-13T21:59:42Z")

</div>

Ack, what I explained WAS array.in\_groups\_of(n).

What I'm ACTUALLY looking for is: array.nest\_by(2) =\> [[a, b, c, 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]]

Where nest\_by(n) produces n nested arrays. In the case of 2 and 4 when it's not an balanced match, distribute the extras amongst first arrays.

---

<div class="post-metadata">

**Author:** ![John\_Barnette](https://avatars.discourse-cdn.com/v4/letter/j/ecd19e/32.png) [@John\_Barnette](https://discuss.rubyonrails.org/u/John_Barnette)\
**Post date:** [July 13, 2007, 11:58pm UTC](https://discuss.rubyonrails.org/t/tricky-ruby-array-grouping-method/15636/3 "2007-07-13T23:58:57Z")

</div>

This is probably better posted in ruby-talk. Here's a stab, though:

class Array &nbsp;&nbsp;def nest\_by(group\_count) &nbsp;&nbsp;&nbsp;&nbsp;in\_groups\_of(size / group\_count + size % group\_count).collect(&:compact) &nbsp;&nbsp;end end

~ j.

---

<div class="post-metadata">

**Author:** ![Gene\_Tani](https://avatars.discourse-cdn.com/v4/letter/g/4491bb/32.png) [@Gene\_Tani](https://discuss.rubyonrails.org/u/Gene_Tani)\
**Post date:** [July 14, 2007, 5:30am UTC](https://discuss.rubyonrails.org/t/tricky-ruby-array-grouping-method/15636/4 "2007-07-14T05:30:19Z")

</div>

you could probably golf this, but:

require 'enumerator' class Array &nbsp;&nbsp;&nbsp;&nbsp;def nest\_by(length\_of\_out) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;num\_per\_element, remainder = self.length.divmod(length\_of\_out); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;temp= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.reverse.each\_slice(num\_per\_element) { |slice| temp \<\< slice} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if remainder\>0 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;temp[-2]+= temp[-1] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;temp.slice!(-1) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return temp.reverse &nbsp;&nbsp;&nbsp;&nbsp;end end

---

<div class="post-metadata">

**Author:** ![Daniel\_Lunde](https://avatars.discourse-cdn.com/v4/letter/d/c89c15/32.png) [@Daniel\_Lunde](https://discuss.rubyonrails.org/u/Daniel_Lunde)\
**Post date:** [July 16, 2007, 8:08pm UTC](https://discuss.rubyonrails.org/t/tricky-ruby-array-grouping-method/15636/5 "2007-07-16T20:08:46Z")

</div>

Thanks gene.tani, you got me close enough. The only problem with your solution was that if there were 2 remaining elements, I wanted those distributed among the first two nested array, not all in the first array. Here's the finished solution for what I was looking for:

class Array &nbsp;&nbsp;def in\_n\_groups(n) &nbsp;&nbsp;&nbsp;&nbsp;num\_per\_element, remainder = self.length.divmod(n); &nbsp;&nbsp;&nbsp;&nbsp;orig = self.dup &nbsp;&nbsp;&nbsp;&nbsp;temp = &nbsp;&nbsp;&nbsp;&nbsp;until orig.empty? &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if remainder \> 0 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;temp \<\< orig.slice!(0, num\_per\_element+1) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;remainder -= 1 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;temp \<\< orig.slice!(0, num\_per\_element) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;&nbsp;&nbsp;return temp &nbsp;&nbsp;end end

array = %w{ a b c d e f g h i j k } array =\> ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k"]

array.in\_n\_groups(2) =\> [["a", "b", "c", "d", "e", "f"], ["g", "h", "i", "j", "k"]]

array.in\_n\_groups(3) =\> [["a", "b", "c", "d"], ["e", "f", "g", "h"], ["i", "j", "k"]]

array.in\_n\_groups(4) =\> [["a", "b", "c"], ["d", "e", "f"], ["g", "h", "i"], ["j", "k"]]

Great for distributing long lists of data between balanced table columns. Especially when you only want n columns.

Thanks everyone for helping.

Daniel
