Unbuffered Read from File--Ruby

293 views Asked by At

I need a way to read from a file, but reloading the data from the disk each time. How can this be done, short of using File.reopen every time?

1

There are 1 answers

2
mu is too short On BEST ANSWER

You could use IO#rewind:

fp = File.open('pancakes.txt')
s  = fp.read
# Something changes the first part pancakes.txt...
fp.rewind
s = fp.read # This reads again from the beginning

This does of course require a seekable file but that shouldn't be a problem if you're using plain disk files.