Bharat Ruparel wrote: > I am new to ruby and am trying to learn my way around it. I saw the > following example somewhere on the net to read a file line by line and > list it to the console: > > IO.foreach("test.txt") {|line| puts line} > > This works great. However, I am trying to take the next step(s). For > starters, I would like to modify the above code such that it lets me > prepend the line numbers to each line as it is listed to the console. > Something similar to the listing below for example: > > 1. First line from the file.. > 2. Second line from the file... > .. > .. > 12. Last line from the file. > > What is the Ruby idiom for doing this? Using the iterator and the code > block? > > Thanks in advance. > Regards, > > Bharat > > counter = 1 IO.foreach("test.txt") {|line| puts "#{counter}. #{line}"; counter += 1}