Reading the last n lines of a file in Ruby?
If on a *nix system with tail
, you can cheat like this:
last_25_lines = `tail -n 25 whatever.txt`
Is the file large enough that you need to avoid reading the whole thing? If not, you could just do
IO.readlines("file.log")[-25..-1]
If it is to big, you may need to use IO#seek
to read from near the end of the file, and continue seeking toward the beginning until you've seen 25 lines.