tags:

views:

182

answers:

2

Using Ruby, I am reading a file line by line, using IO.gets to incrementally read the next line of the file. Under certain circumstances I want to do the opposite (look at the previous line by decrementing). The way I tried to accomplish this was...

IO.lineno = int
IO.gets

It seems that no matter what I set "lineno" to equal it still just reads the next line when I follow up by calling "gets". How should I go about reading previous lines in the file?

+1  A: 

You need to use

IO.readlines("myfile")

This returns the file as an array of strings and then iterate over it with indizies. With a stream there is no way to go back one line.

Lothar
A: 

give elif a look

Martin DeMello