From: "W. James" Date: 2009-12-09T17:55:11+09:00 Subject: Re: indent strings(make them lined up) Jon Kim wrote: > Hi, > > I am newbie in Ruby. I have a question about indenting string. > > there is strings like below: > > www.google.com => 123.123.123.12 > www.ibm.com => 123.112.123.12 > www.iowahawkeyes => 123.123.112.12 > > i want to make above like next > > www.google.com => 123.123.123.12 > www.ibm.com => 123.112.123.12 > www.iowahawkeyes => 123.123.112.12 > > > original code was > > puts (dns + " => " + ip) > > I tried next code > > puts (dns + " "*(20 - dns.length) + " = > " + ip) > > but it did not work well. please teach me how to do it. data = %w(blip bleeping rob_the_fraud tor what-pompous-verbosity dreadful). map{|s| [s + '.com', (1..4).map{rand 256}.join('.')] } width = data.map{|a,b| a.size}.max puts data.map{|a,b| [a.ljust(width),b].join " => " } === output === blip.com => 249.7.84.219 bleeping.com => 14.120.94.124 rob_the_fraud.com => 252.63.171.160 tor.com => 173.239.40.90 what-pompous-verbosity.com => 56.61.184.48 dreadful.com => 31.56.145.209 --