--001485f03cac3ec177048d7bee7c
Content-Type: text/plain; charset=ISO-8859-1

On Tue, Aug 10, 2010 at 10:51 AM, David Ainley <wrinkliez / gmail.com> wrote:

> Hey guys.  I have an array full of strings.  When I put the strings, it
> looks like this 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
> 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/.
>
>

# 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  rray.new
  array_of_arrays_of_strings.each do |strings|
    strings.each_with_index do |string,index|
      widths[index]  tring.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  et_widths data # [9, 14, 19, 12]

# turn the widths into a format string (contains the spacing information)
format  idths.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)

--001485f03cac3ec177048d7bee7c--