From: David Alan Black Date: 2001-07-27T06:19:16+09:00 Subject: [ruby-talk:18598] Re: My first Ruby program... :-) Hello -- On Fri, 27 Jul 2001, Bjorn Pettersen wrote: > 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. I used your program as an excuse to explore class Matrix a little more. I don't have any strong recommendation about whether to use Matrix for your purposes, though it might keep things manageable if your program grows. Then again, at least in this version, there's a fair amount of flipping among types, which probably slows it down. (Optimizations and streamlinings are welcome :-) Anyway, for your hacking pleasure, here is a Matrix-based rewrite. Note that creating a matrix involves a call to #[] -- for example, m = Matrix[ [1,2], [3,4] ].) require 'matrix' class Array def rpad m = map {|e| e.to_s.size} .max + 1 dup.map do |e| e.to_s.ljust(m) end end end class StringTable < Matrix def to_s res = "" m = (0...column_size).map do |n| column(n).to_a.rpad end type[*m].transpose.to_a.each do |r| res << r.join + "\n" end res end end def test1 lst = [ %w{ This is a hack of the }, %w{ program which Bjorn put on the }, %w{ Ruby mailing list earlier this afternoon.} ] puts StringTable[*lst] end test1 David -- David Alan Black home: dblack@candle.superlink.net work: blackdav@shu.edu Web: http://pirate.shu.edu/~blackdav