From: Thomas Adam Date: 2008-02-14T08:13:41+09:00 Subject: Re: my noob "numbering lines" programe. How to improve? Hi -- On 13/02/2008, 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. If all you're doing is inserting line numbers in front of the lines in that file then: f=File.open("./file").readlines lines=0 f.each {|content| puts "#{lines += 10}: #{content}" }' In its crudest form will do that, but note that it doesn't modify anything, it just dumps the values out. One other approach is to modify an array of those values "in-place" instead, as in: f=File.open("./.xsession").readlines f.each {|e| e.sub!(/^/, "#{f.index(e) + 10}: ")} puts f This "cheats" and uses the index of the element to form the line number - adding it to the start of the string element. Saving the resultant array "f" to a file, per the second example is left an exercise to the reader, as is any further cosmetic formatting to the output. -- Thomas Adam