Read from middle/near end of files?

Does anyone know if it is possible to read say the last 10 line of a
file? readlines would work if memory is unlimited, but that isn't the
case, especially if the file is a 2GB log file. Or is this not possible
at all?

Never used it, but a google search for 'ruby file tail' turns up this:

You could also get the size of the file, open it, and then seek to somewhere near the end and just start reading at that point till you reach the end...


I *have* used file-tail and taken a look at the code. It seems to be well written and covers possible gotchas quite well. For example, to get the revision number out of the revisions.log file that capistrano adds a line to on each deploy:

         rev = do |f|
           f.rewind(1).gets.match(/(\d+) \d{14}$/)[1]

You gotta like that!


Rob Biedenharn