From: Robert Klemme Date: 2010-07-23T03:07:55+09:00 Subject: Re: finding last line in a file On 22.07.2010 16:22, Urabe Shyouhei wrote: > (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. >> 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. Good point. Although pragmatically for fixed width and UTF-8 encoded files it will work and the performance gains for large files are significant enough to warrant this approach. Cheers robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/