# Grouping an Array

**URL:** <https://rubytalk.org/t/grouping-an-array/1553>\
**Category:** ruby-talk\
**Created:** [3 September 2002 23:04 UTC](https://rubytalk.org/t/grouping-an-array/1553 "2002-09-03T23:04:25Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bruce\_Williams](https://avatars.discourse-cdn.com/v4/letter/b/d9b06d/32.png) [@Bruce\_Williams](https://rubytalk.org/u/Bruce_Williams)\
**Post date:** [3 September 2002 23:04 UTC](https://rubytalk.org/t/grouping-an-array/1553/1 "2002-09-03T23:04:25Z")

</div>

I find myself using the following snippet often when grouping an Array into  
smaller arrays of [less than or equal to] a certain number for various  
purposes.

class Array  
def group threshold  
result = []  
self.each do |item|  
begin  
result \<\< [] if result.last.length == threshold  
rescue NameError  
result \<\< []  
end  
result.last \<\< item  
end  
result  
end  
end

This allows me to do the following:

irb(main):002:0\> [1,2,3,4,5,6,7,8,9,10].group 3  
[[1, 2, 3], [4, 5, 6], [7, 8, 9], [10]]

Is there a better way to do this?

- B

> **···**
>
> –  
> Bruce Williams [http://www.codedbliss.com](http://www.codedbliss.com)  
> iusris/#ruby-lang[bruce@codedbliss.com](mailto:bruce@codedbliss.com)

---

<div class="post-metadata">

**Author:** ![David\_Alan\_Black1](https://avatars.discourse-cdn.com/v4/letter/d/278dde/32.png) [@David\_Alan\_Black1](https://rubytalk.org/u/David_Alan_Black1)\
**Post date:** [3 September 2002 23:13 UTC](https://rubytalk.org/t/grouping-an-array/1553/2 "2002-09-03T23:13:07Z")

</div>

Hello –

> I find myself using the following snippet often when grouping an Array into  
> smaller arrays of [less than or equal to] a certain number for various  
> purposes.
> 
> class Array  
> def group threshold  
> result =   
> self.each do |item|  
> begin  
> result \<\< if result.last.length == threshold  
> rescue NameError  
> result \<\<   
> end  
> result.last \<\< item  
> end  
> result  
> end  
> end
> 
> This allows me to do the following:
> 
> irb(main):002:0\> [1,2,3,4,5,6,7,8,9,10].group 3  
> [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10]]
> 
> Is there a better way to do this?

I don’t know if it’s better, but just to share the incarnation  
of this that I’ve got in my scraps-of-code library:

class Array  
def in\_slices\_of(n)  
res =   
0.step(size - 1, n) do |i|  
s = slice(i … i + n)  
yield s if block\_given?  
res.push s  
end  
res  
end  
end

[1,2,3,4,5,6,7,8,9,10].in\_slices\_of(3)

# =\> [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10]]

The yielding was just in case one wanted to do something with each  
sub-array in a block.

David

> **···**
>
> On Wed, 4 Sep 2002, Bruce Williams wrote:
> 
> –  
> David Alan Black | Register for RubyConf 2002!  
> home: [dblack@candle.superlink.net](mailto:dblack@candle.superlink.net) | November 1-3  
> work: blackdav@shu.edu | Seattle, WA, USA  
> Web: [http://pirate.shu.edu/~blackdav](http://pirate.shu.edu/~blackdav) | [http://www.rubyconf.com](http://www.rubyconf.com)

---

<div class="post-metadata">

**Author:** ![Bruce\_Williams](https://avatars.discourse-cdn.com/v4/letter/b/d9b06d/32.png) [@Bruce\_Williams](https://rubytalk.org/u/Bruce_Williams)\
**Post date:** [3 September 2002 23:24 UTC](https://rubytalk.org/t/grouping-an-array/1553/3 "2002-09-03T23:24:24Z")

</div>

I like the yielding. I think I’ll add that. 🙂

> **···**
>
> On Tuesday 03 September 2002 07:13 pm, [dblack@candle.superlink.net](mailto:dblack@candle.superlink.net) wrote:
> 
> > The yielding was just in case one wanted to do something with each  
> > sub-array in a block.
> > 
> > David
> 
> –  
> Bruce Williams [http://www.codedbliss.com](http://www.codedbliss.com)  
> iusris/#ruby-lang[bruce@codedbliss.com](mailto:bruce@codedbliss.com)
