From: Bjorn Pettersen Date: 2001-07-27T02:52:05+09:00 Subject: [ruby-talk:18583] My first Ruby program... :-) Well, it's now two days into playing with Ruby, and I wrote my first non-trivial program (it's converting a large c++ enum to constants). I'm still shaky on idiomatic usage, so if any of the initiated would care to comment on coding style I would be very grateful. -- bjorn # small class to take a list of lists of strings # and output them in nicely adjusted columns class StringTable def initialize(table) @table = table @ncols = table[0].length @maxcols = [].fill(0, 0...@ncols) #create a vector of max lenghts of each column @table.each {|row| (0...row.length).each {|i| @maxcols[i] = [row[i].length, @maxcols[i]].max } } end def output # create the format specifier fmt = '' (0...@maxcols.length).each {|n| fmt += "%-#{@maxcols[n]+1}s" } fmt += "\n" @table.each {|row| printf(fmt, *row) } end end def test lst = [] IO.foreach('geneid.in') {|line| if line =~ /\s*(\w+)\s*=\s*(\d+)/ lst << ['const long', $1, '=', $2+';'] end } st = StringTable.new(lst) st.output end test