Unbuffered Read from File--Ruby
-
22-04-2021 - |
Question
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?
Solution
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.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow