# Spacing data into columns

**URL:** <https://www.ruby-forum.com/t/spacing-data-into-columns/141212>\
**Category:** Ruby\
**Created:** [June 23, 2008, 6:33pm UTC](https://www.ruby-forum.com/t/spacing-data-into-columns/141212 "2008-06-23T18:33:39Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jturney](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e99b99/32.png) [@jturney](https://www.ruby-forum.com/u/jturney)\
**Post date:** [June 23, 2008, 6:33pm UTC](https://www.ruby-forum.com/t/spacing-data-into-columns/141212/1 "2008-06-23T18:33:39Z")

</div>

Hi, I want to output a series of items, but I want to make sure they’re  
properly spaced out so it’s more readable.

Sample:

dfssdf | 393f | dfskjsdfk  
dfjkdfkdfkj| fd | 3493  
df | 1 | etc…

Each row is a class and the data is a variable in the class. For  
example, row 1-variable 1 is ‘dfssdf’, row1-variable 2 is ‘393f’, etc.

All of my row objects are stored in an array, and when I want to output  
them, I iterate through the array to access each object’s data. How can  
I make sure that each object’s data is output in accordance with every  
other data’s data? (i.e. proper spacing)

THANKS!

---

<div class="post-metadata">

**Author:** ![jturney](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e99b99/32.png) [@jturney](https://www.ruby-forum.com/u/jturney)\
**Post date:** [June 23, 2008, 6:43pm UTC](https://www.ruby-forum.com/t/spacing-data-into-columns/141212/2 "2008-06-23T18:43:10Z")

</div>

On Jun 23, 2008, at 10:31 AM, Justin To wrote:

> Each row is a class and the data is a variable in the class. For  
> example, row 1-variable 1 is ‘dfssdf’, row1-variable 2 is ‘393f’, etc.
> 
> All of my row objects are stored in an array, and when I want to  
> output  
> them, I iterate through the array to access each object’s data. How  
> can  
> I make sure that each object’s data is output in accordance with every  
> other data’s data? (i.e. proper spacing)
> 
> THANKS!

# compute widths expensively

widths = []

rows.each do |row|  
row.each\_with\_index do |cell, idx|  
widths[idx] = [cell.to\_s.size, widths[idx] || 0].max  
end  
end

# use them

rows.each do |row|  
formatted = []  
row.each\_with\_index do |cell, idx|  
width = widths[idx]  
format = “%-#{ width }.#{ width }s”  
formatted \<\< (format % cell)  
end  
puts formatted.join(’ | ')  
end

off the top of my head and super inefficient, but the foundation of  
what you need to do. pre-compute where possible.

regards.  
a @ [http://codeforpeople.com/](http://codeforpeople.com/)

---

<div class="post-metadata">

**Author:** ![jturney](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e99b99/32.png) [@jturney](https://www.ruby-forum.com/u/jturney)\
**Post date:** [June 24, 2008, 1:00pm UTC](https://www.ruby-forum.com/t/spacing-data-into-columns/141212/3 "2008-06-24T13:00:53Z")

</div>

[http://api.rubyreports.org/](http://api.rubyreports.org/)

If you’re going to do a lot of this sort of thing.

---

<div class="post-metadata">

**Author:** ![jturney](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e99b99/32.png) [@jturney](https://www.ruby-forum.com/u/jturney)\
**Post date:** [June 24, 2008, 1:11pm UTC](https://www.ruby-forum.com/t/spacing-data-into-columns/141212/4 "2008-06-24T13:11:26Z")

</div>

2008/6/23 ara.t.howard [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> > df | 1 | etc…
> > 
> > Each row is a class and the data is a variable in the class. For  
> > example, row 1-variable 1 is ‘dfssdf’, row1-variable 2 is ‘393f’, etc.
> > 
> > All of my row objects are stored in an array, and when I want to output  
> > them, I iterate through the array to access each object’s data. How can  
> > I make sure that each object’s data is output in accordance with every  
> > other data’s data? (i.e. proper spacing)

> off the top of my head and super inefficient, but the foundation of what you  
> need to do. pre-compute where possible.

Here’s another approach - a bit more lightweight but you need  
knowledge about max widths:

irb(main):001:0\> data = [  
irb(main):002:1\* %w{abc def ghi},  
irb(main):003:1\* %w{foo bar baz}  
irb(main):004:1\> ]  
=\> [[“abc”, “def”, “ghi”], [“foo”, “bar”, “baz”]]  
irb(main):005:0\> data.each do |row|  
irb(main):006:1\* printf “%-10s | %-10s | %-10s\n”, \*row  
irb(main):007:1\> end  
abc | def | ghi  
foo | bar | baz  
=\> [[“abc”, “def”, “ghi”], [“foo”, “bar”, “baz”]]

Kind regards

robert
