0

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?

Linuxios
  • 34,849
  • 13
  • 91
  • 116

1 Answers1

1

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.

mu is too short
  • 426,620
  • 70
  • 833
  • 800
  • This will reload contents from disk? This file will be modified by other processes, and every time I read, I want the most recent data. – Linuxios Feb 03 '12 at 04:38
  • @Linux_iOS.rb.cpp.c.lisp.m.sh: Yes, it moves the read pointer back to the beginning and starts again with whatever is in the file. You can try it `irb` and you'll see. – mu is too short Feb 03 '12 at 04:43