# Changing the spacing in a string?

**URL:** <https://www.ruby-forum.com/t/changing-the-spacing-in-a-string/193067>\
**Category:** Ruby\
**Created:** [August 10, 2010, 5:51pm UTC](https://www.ruby-forum.com/t/changing-the-spacing-in-a-string/193067 "2010-08-10T17:51:27Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![mickey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ccd318/32.png) [@mickey](https://www.ruby-forum.com/u/mickey)\
**Post date:** [August 10, 2010, 5:51pm UTC](https://www.ruby-forum.com/t/changing-the-spacing-in-a-string/193067/1 "2010-08-10T17:51:27Z")

</div>

Hey guys. I have an array full of strings. When I put the strings, it  
looks like this [gm-notify 0.10.2-1~ppa1 AlexanderHungenberg (2010-05-05)gm-notify 0.9-0~p - Pastebin.com](http://pastebin.com/eeicUCqL) which is okay, but it looks  
a little sloppy to me. Is there a way I can get the strings to  
dynamically space themselves so that they look evenly spaced? ala  
[gm-notify 0.10.2-1~ppa1 AlexanderHungenberg (2010-05-05)gm-notify 0.9-0~p - Pastebin.com](http://pastebin.com/cRcG97sK) ? Should I split each string into it’s  
separate parts, and then print them via format? Or something…?

---

<div class="post-metadata">

**Author:** ![mickey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ccd318/32.png) [@mickey](https://www.ruby-forum.com/u/mickey)\
**Post date:** [August 10, 2010, 5:56pm UTC](https://www.ruby-forum.com/t/changing-the-spacing-in-a-string/193067/2 "2010-08-10T17:56:50Z")

</div>

On Tue, Aug 10, 2010 at 8:51 AM, David A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Hey guys. I have an array full of strings. When I put the strings, it  
> looks like this [gm-notify 0.10.2-1~ppa1 AlexanderHungenberg (2010-05-05)gm-notify 0.9-0~p - Pastebin.com](http://pastebin.com/eeicUCqL) which is okay, but it looks  
> a little sloppy to me. Is there a way I can get the strings to  
> dynamically space themselves so that they look evenly spaced? ala  
> [gm-notify 0.10.2-1~ppa1 AlexanderHungenberg (2010-05-05)gm-notify 0.9-0~p - Pastebin.com](http://pastebin.com/cRcG97sK) ? Should I split each string into it’s  
> separate parts, and then print them via format? Or something…?

You’ve got a few options, depending on how sophisticated you want to  
get. Based on that small sample, it looks like the easiest thing to  
do might be to replace runs of multiple spaces with a single tab.  
Something like:

str.gsub( /\s+/, “\t” )

Otherwise, you could definitely split it on spaces and print using  
#printf and a carefully-crafted format string.

Ben

---

<div class="post-metadata">

**Author:** ![mickey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ccd318/32.png) [@mickey](https://www.ruby-forum.com/u/mickey)\
**Post date:** [August 10, 2010, 7:19pm UTC](https://www.ruby-forum.com/t/changing-the-spacing-in-a-string/193067/3 "2010-08-10T19:19:18Z")

</div>

Ben B. wrote:

> On Tue, Aug 10, 2010 at 8:51 AM, David A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> wrote:
> 
> > Hey guys. ï¿½I have an array full of strings. ï¿½When I put the strings, it  
> > looks like this [gm-notify 0.10.2-1~ppa1 AlexanderHungenberg (2010-05-05)gm-notify 0.9-0~p - Pastebin.com](http://pastebin.com/eeicUCqL) which is okay, but it looks  
> > a little sloppy to me. ï¿½Is there a way I can get the strings to  
> > dynamically space themselves so that they look evenly spaced? ala  
> > [gm-notify 0.10.2-1~ppa1 AlexanderHungenberg (2010-05-05)gm-notify 0.9-0~p - Pastebin.com](http://pastebin.com/cRcG97sK) ? ï¿½Should I split each string into it’s  
> > separate parts, and then print them via format? ï¿½Or something…?
> 
> You’ve got a few options, depending on how sophisticated you want to  
> get. Based on that small sample, it looks like the easiest thing to  
> do might be to replace runs of multiple spaces with a single tab.  
> Something like:
> 
> str.gsub( /\s+/, “\t” )
> 
> Otherwise, you could definitely split it on spaces and print using  
> #printf and a carefully-crafted format string.
> 
> Ben

Thanks for the response! It seems to be working, except for the two  
packages that deal with AlexanderHungenberg. With one tab, his name is  
too long for the date to be formatted correctly (  
[http://pastebin.com/g8gEcvhJ](http://pastebin.com/g8gEcvhJ) ). I thought that having two tabs instead  
of one might fix the problemm but then the date is still jutted out (  
[http://pastebin.com/CB13TZ6E](http://pastebin.com/CB13TZ6E) ). This isn’t that massive of a problem  
(Alexander just has a unconditionally long username), but, anyways, any  
ideas?

---

<div class="post-metadata">

**Author:** ![mickey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ccd318/32.png) [@mickey](https://www.ruby-forum.com/u/mickey)\
**Post date:** [August 10, 2010, 8:02pm UTC](https://www.ruby-forum.com/t/changing-the-spacing-in-a-string/193067/4 "2010-08-10T20:02:42Z")

</div>

On Tue, Aug 10, 2010 at 10:51 AM, David A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> ## Hey guys. I have an array full of strings. When I put the strings, it looks like this [gm-notify 0.10.2-1~ppa1 AlexanderHungenberg (2010-05-05)gm-notify 0.9-0~p - Pastebin.com](http://pastebin.com/eeicUCqL) which is okay, but it looks a little sloppy to me. Is there a way I can get the strings to dynamically space themselves so that they look evenly spaced? ala [gm-notify 0.10.2-1~ppa1 AlexanderHungenberg (2010-05-05)gm-notify 0.9-0~p - Pastebin.com](http://pastebin.com/cRcG97sK) ? Should I split each string into it’s separate parts, and then print them via format? Or something…?
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

# counts the length of each string based on column

# returns the max for each column across all rows

def get\_widths(array\_of\_arrays\_of\_strings)  
widths = Array.new  
array\_of\_arrays\_of\_strings.each do |strings|  
strings.each\_with\_index do |string,index|  
widths[index] = string.length if !widths[index] || widths[index] \<  
string.length  
end  
end  
widths  
end

# the data itself – you haven’t show what format it is in, this is just

a  
guess  
data = [  
‘gm-notify 0.10.2-1~ppa1 AlexanderHungenberg (2010-05-05)’,  
‘gm-notify 0.9-0~ppa6 TomVetterlein (2009-05-15)’ ,  
‘gm-notify 0.10.2-1~ppa1 AlexanderHungenberg (2010-05-13)’,  
‘gm-notify 0.9+r39-1~ppa1 SeanStoops (2009-12-22)’ ,  
‘gm-notify 0.9-0~ppa7 NikolaKovacs (2009-09-27)’ ,  
‘gm-notify 0.8-0ppa2 TomVetterlein (2009-04-27)’ ,  
]

# split the strings on whitespace so they are now arrays of strings

data.map!(&:split)

# get an array of max widths for each row

widths = get\_widths data # =\> [9, 14, 19, 12]

# turn the widths into a format string (contains the spacing

information)  
format = widths.map { |width| “%-#{width}s” }.join(’ ')  
format # =\> “%-9s %-14s %-19s %-12s”

# format and output the strings

data.each { |strings| puts format % strings }

# \>\> gm-notify 0.10.2-1~ppa1 AlexanderHungenberg (2010-05-05)

# \>\> gm-notify 0.9-0~ppa6 TomVetterlein (2009-05-15)

# \>\> gm-notify 0.10.2-1~ppa1 AlexanderHungenberg (2010-05-13)

# \>\> gm-notify 0.9+r39-1~ppa1 SeanStoops (2009-12-22)

# \>\> gm-notify 0.9-0~ppa7 NikolaKovacs (2009-09-27)

# \>\> gm-notify 0.8-0ppa2 TomVetterlein (2009-04-27)

---

<div class="post-metadata">

**Author:** ![mickey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ccd318/32.png) [@mickey](https://www.ruby-forum.com/u/mickey)\
**Post date:** [August 10, 2010, 8:56pm UTC](https://www.ruby-forum.com/t/changing-the-spacing-in-a-string/193067/5 "2010-08-10T20:56:00Z")

</div>

On Tue, Aug 10, 2010 at 11:51 AM, David A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Hey guys. I have an array full of strings. When I put the strings, it  
> looks like this [gm-notify 0.10.2-1~ppa1 AlexanderHungenberg (2010-05-05)gm-notify 0.9-0~p - Pastebin.com](http://pastebin.com/eeicUCqL) which is okay, but it looks  
> a little sloppy to me. Is there a way I can get the strings to  
> dynamically space themselves so that they look evenly spaced? ala  
> [gm-notify 0.10.2-1~ppa1 AlexanderHungenberg (2010-05-05)gm-notify 0.9-0~p - Pastebin.com](http://pastebin.com/cRcG97sK) ? Should I split each string into it’s  
> separate parts, and then print them via format? Or something…?

And for a different approach 🙂

/tmp\> cat i.rb  
str = Array.new  
str \<\< “gm-notify 0.10.2-1~ppa1 AlexanderHungenberg (2010-05-05)”  
str \<\< “gm-notify 0.9-0~ppa6 TomVetterlein (2009-05-15)”  
str \<\< “gm-notify 0.10.2-1~ppa1 AlexanderHungenberg (2010-05-13)”  
str \<\< “gm-notify 0.9+r39-1~ppa1 SeanStoops (2009-12-22)”  
str \<\< “gm-notify 0.9-0~ppa7 NikolaKovacs (2009-09-27)”  
str \<\< “gm-notify 0.8-0ppa2 TomVetterlein (2009-04-27)”

x = IO.popen(“column -t”, “w+”){|p|  
p.puts str.join(“\n”)  
p.close\_write  
p.read  
}

puts x

/tmp\> ruby i.rb  
gm-notify 0.10.2-1~ppa1 AlexanderHungenberg (2010-05-05)  
gm-notify 0.9-0~ppa6 TomVetterlein (2009-05-15)  
gm-notify 0.10.2-1~ppa1 AlexanderHungenberg (2010-05-13)  
gm-notify 0.9+r39-1~ppa1 SeanStoops (2009-12-22)  
gm-notify 0.9-0~ppa7 NikolaKovacs (2009-09-27)  
gm-notify 0.8-0ppa2 TomVetterlein (2009-04-27)

---

<div class="post-metadata">

**Author:** ![mickey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ccd318/32.png) [@mickey](https://www.ruby-forum.com/u/mickey)\
**Post date:** [August 10, 2010, 8:58pm UTC](https://www.ruby-forum.com/t/changing-the-spacing-in-a-string/193067/6 "2010-08-10T20:58:56Z")

</div>

Sweet! 🙂
