# Real life use of each\_cons? (... follow up)

**URL:** <https://www.ruby-forum.com/t/real-life-use-of-each-cons-follow-up/188722>\
**Category:** Ruby\
**Created:** [May 17, 2010, 11:10am UTC](https://www.ruby-forum.com/t/real-life-use-of-each-cons-follow-up/188722 "2010-05-17T11:10:37Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![jakub](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ecb155/32.png) [@jakub](https://www.ruby-forum.com/u/jakub)\
**Post date:** [May 17, 2010, 11:10am UTC](https://www.ruby-forum.com/t/real-life-use-of-each-cons-follow-up/188722/1 "2010-05-17T11:10:37Z")

</div>

Hi there!

(following up the discussion from last year)

This method actually made my life a lot easier when fitting some data  
to a table. I had tens of pictures and wanted to show them in rows -  
three columns each.

data.each\_cons(3) do |column|  
column.each do |picture|  
show picture  
end  
end

I was playing with indices, modulos and stuff. After refactoring with  
each\_cons, the code looked much cleaner.

cheers!

---

<div class="post-metadata">

**Author:** ![jakub](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ecb155/32.png) [@jakub](https://www.ruby-forum.com/u/jakub)\
**Post date:** [May 17, 2010, 12:20pm UTC](https://www.ruby-forum.com/t/real-life-use-of-each-cons-follow-up/188722/2 "2010-05-17T12:20:52Z")

</div>

Hi –

On Mon, 17 May 2010, Jakub wrote:

> show picture  
> end  
> end
> 
> I was playing with indices, modulos and stuff. After refactoring with  
> each\_cons, the code looked much cleaner.

Are you sure you don’t want each\_slice rather than each\_cons? With  
each\_cons you’ll get repetition:

array = [1,2,3,4,5,6,7,8,9]  
array.each\_cons(3) {|c| p c }

```
 [1, 2, 3]
 [2, 3, 4]
 [3, 4, 5]
 etc.

```

David

–  
David A. Black, Senior Developer, Cyrus Innovation Inc.

THE Ruby training with Black/Brown/McAnally  
COMPLEAT Coming to Chicago area, June 18-19, 2010!  
RUBYIST [http://www.compleatrubyist.com](http://www.compleatrubyist.com)
