From: Urabe Shyouhei Date: 2010-07-22T23:22:00+09:00 Subject: Re: finding last line in a file (2010/07/22 22:06), Robert Klemme wrote: >> Then how about: >> >> File.open(f).each_line.inject(nil){|x,y|y} >> >> Slower than my first answer though. > > This still reads in the whole file plus you do not close the File > object properly. I'm sure you know how to do that properly. > A solution that is efficient for large files will > use File#seek to move position to the end of the file and go backwards > from there. No, that can't work -- or that works only when you are sticking on the ASCII character set. Multibyte encodings are not safe to read backwards (sometimes). The only safe way is to read the whole content from the beginning, at least one time.