From: William James Date: 2008-02-15T02:09:57+09:00 Subject: Re: my noob "numbering lines" programe. How to improve? On Feb 13, 4:50 pm, Adam Akhtar wrote: > Give us hints on how to improve this program. If you want to show your > code off then go ahead but please give us the hints first i.e. what > methods you would use, what you would rewrite in my code etc. Its so > much better for me to go away and try and replicate your thinking than > just reading it. > > heres my version > > #program to insert numbers infront of lines. > def numbertext(infilename, outfilename) > #for neat formating work out how many lines there are first so we know > how #many digits to set ljust to > totalnumlines = File.readlines(infilename).length > numdigits = 1 > x = 1 > while (x < totalnumlines) > x = x * 10 > numdigits = numdigits + 1 > end > > linenumber = 0 > File.open(outfilename, "w") do |output| > File.foreach(infilename) do |input| > output.write linenumber.to_s.rjust(numdigits) + " " + input > linenumber = linenumber + 1 > end > end > end > -- > Posted viahttp://www.ruby-forum.com/. a = IO.readlines('data') puts (1..a.size).map{|x| x.to_s.rjust(a.size.to_s.size)}.zip(a). map{|x| x.join ' '}