# How to the output in 12x8 format

**URL:** <https://www.ruby-forum.com/t/how-to-the-output-in-12x8-format/72929>\
**Category:** Ruby\
**Created:** [October 19, 2006, 11:35pm UTC](https://www.ruby-forum.com/t/how-to-the-output-in-12x8-format/72929 "2006-10-19T23:35:39Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [October 19, 2006, 11:35pm UTC](https://www.ruby-forum.com/t/how-to-the-output-in-12x8-format/72929/1 "2006-10-19T23:35:39Z")

</div>

Hi folks,

I have an array containing 96 elements. I want to print the array in  
12x8 format. Any comments?

Thanks in advance,

Li

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [October 19, 2006, 11:45pm UTC](https://www.ruby-forum.com/t/how-to-the-output-in-12x8-format/72929/2 "2006-10-19T23:45:51Z")

</div>

On 10/19/06, Li Chen [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi folks,
> 
> I have an array containing 96 elements. I want to print the array in  
> 12x8 format. Any comments?
> 
> Thanks in advance,
> 
> Li

require ‘enumerator’

ary = (1…96).to\_a  
ary.each\_slice(8) {|slice| puts slice.inspect}

[1, 2, 3, 4, 5, 6, 7, 8]  
[9, 10, 11, 12, 13, 14, 15, 16]  
[17, 18, 19, 20, 21, 22, 23, 24]  
[25, 26, 27, 28, 29, 30, 31, 32]  
[33, 34, 35, 36, 37, 38, 39, 40]  
[41, 42, 43, 44, 45, 46, 47, 48]  
[49, 50, 51, 52, 53, 54, 55, 56]  
[57, 58, 59, 60, 61, 62, 63, 64]  
[65, 66, 67, 68, 69, 70, 71, 72]  
[73, 74, 75, 76, 77, 78, 79, 80]  
[81, 82, 83, 84, 85, 86, 87, 88]  
[89, 90, 91, 92, 93, 94, 95, 96]

Blessings,  
TwP

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [October 20, 2006, 12:07am UTC](https://www.ruby-forum.com/t/how-to-the-output-in-12x8-format/72929/3 "2006-10-20T00:07:54Z")

</div>

-----BEGIN PGP SIGNED MESSAGE-----  
Hash: SHA1

Li Chen wrote:  
| I have an array containing 96 elements. I want to print the array  
| in 12x8 format. Any comments?

Old school way (Beware! Ternary operator ahead!):

a = (1…96).to\_a.reverse  
a.each\_with\_index{|x,i|print x,i%12==11?"\n":"\t"}

Idea:

Iterate x over all elements of a  
. print x  
. is it value number eleven (the twelveth) in the row?  
. . if so break line  
. . if not jump to next tabulator  
. condition ends  
iteration ends

Jupp  
-----BEGIN PGP SIGNATURE-----  
Version: GnuPG v1.4.5 (GNU/Linux)

iD8DBQFFN/b0rhv7B2zGV08RAnGUAJ0YcmrBhwZzORu0zxzZ7ObuxV1YpwCg4GAk  
dc6aobYd2tKBxQzJ8Wq4Fpk=  
=k96z  
-----END PGP SIGNATURE-----

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [October 20, 2006, 12:37am UTC](https://www.ruby-forum.com/t/how-to-the-output-in-12x8-format/72929/4 "2006-10-20T00:37:56Z")

</div>

On 10/19/06, Li Chen [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > [89, 90, 91, 92, 93, 94, 95, 96]
> 
> Hi,
> 
> Why should I write the code :require ‘enumerator’?
> 
> Is enumerator a module or class?

The ‘each\_slice’ method is defined in the Enumerable module, but  
portions this module are not included by default. Requiring  
enumerator adds some extra methods to the Enumerable objects –  
each\_slice being one of those methods.

Take a look at the Pragmatic book for some more info. It’s on page  
672 of the real book. I could not find any reference to each\_slice in  
the online version.

The rdoc documents aren’t too helpful with this one. They just list  
all the Enumerable methods. It gives the impression that all of them  
are right there in the Array class (which is not the case).

I’m done rambling now. Going home to see the little kiddo and wife 🙂

Blessings,  
TwP

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [October 20, 2006, 12:18am UTC](https://www.ruby-forum.com/t/how-to-the-output-in-12x8-format/72929/5 "2006-10-20T00:18:28Z")

</div>

> require ‘enumerator’
> 
> ary = (1…96).to\_a  
> ary.each\_slice(8) {|slice| puts slice.inspect}
> 
> [1, 2, 3, 4, 5, 6, 7, 8]  
> …

> [89, 90, 91, 92, 93, 94, 95, 96]

Hi,

Why should I write the code :require ‘enumerator’?

Is enumerator a module or class?

Thanks,

Li

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [October 20, 2006, 4:00pm UTC](https://www.ruby-forum.com/t/how-to-the-output-in-12x8-format/72929/6 "2006-10-20T16:00:28Z")

</div>

On Oct 19, 4:35 pm, Li Chen [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi folks,
> 
> I have an array containing 96 elements. I want to print the array in  
> 12x8 format. Any comments?
> 
> Thanks in advance,
> 
> Li

Hi.

The slicing method is neat.  
However, not having the book, this is what i came upon, supposing you’d  
want to keep the string and not only to print it.

class Array  
def print\_format(width)  
str = ‘’  
from\_index = 0  
while !self[from\_index].nil?  
str \<\< self[from\_index, width].inspect + “\n”  
from\_index += width  
end  
str  
end  
end

n = (1…96).to\_a  
puts n.print\_format(8)

Conclusion: I want my book!  
Regards.

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [October 20, 2006, 6:14pm UTC](https://www.ruby-forum.com/t/how-to-the-output-in-12x8-format/72929/7 "2006-10-20T18:14:30Z")

</div>

On 10/19/06, Tim P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> require ‘enumerator’  
> [49, 50, 51, 52, 53, 54, 55, 56]  
> [57, 58, 59, 60, 61, 62, 63, 64]  
> [65, 66, 67, 68, 69, 70, 71, 72]  
> [73, 74, 75, 76, 77, 78, 79, 80]  
> [81, 82, 83, 84, 85, 86, 87, 88]  
> [89, 90, 91, 92, 93, 94, 95, 96]

Or as an alternative:

ary = (1…8).to\_a  
(0…7).each {|i| p ary[i\*8, 8] }

One advantage of Tim’s suggestion of using each\_slice is that the data  
doesn’t really need to be an array, it can be another class which  
mixes in Enumerable.

require ‘enumerator’  
not\_really\_array = (1…96)  
not\_really\_array.each\_slice(8) { |slice| p slice }

[1, 2, 3, 4, 5, 6, 7, 8]  
[9, 10, 11, 12, 13, 14, 15, 16]  
[17, 18, 19, 20, 21, 22, 23, 24]  
[25, 26, 27, 28, 29, 30, 31, 32]  
[33, 34, 35, 36, 37, 38, 39, 40]  
[41, 42, 43, 44, 45, 46, 47, 48]  
[49, 50, 51, 52, 53, 54, 55, 56]  
[57, 58, 59, 60, 61, 62, 63, 64]  
[65, 66, 67, 68, 69, 70, 71, 72]  
[73, 74, 75, 76, 77, 78, 79, 80]  
[81, 82, 83, 84, 85, 86, 87, 88]  
[89, 90, 91, 92, 93, 94, 95, 96]

–  
Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)
