From: Robert Klemme Date: 2010-07-22T22:06:35+09:00 Subject: Re: finding last line in a file 2010/7/22 Urabe Shyouhei : > (2010/07/22 15:35), Robert Klemme wrote: >> On 07/22/2010 07:10 AM, Urabe Shyouhei wrote: >>> (2010/07/22 14:08), Urabe Shyouhei wrote: >>>> Take a look at the doc for File.readline. >>> >>> Oops... typo.  I meant readlines.  With it you can do something like >>> File.readlines(f).last >> >> This is as inefficient as using File.foreach because it will read the >> whole file.  I believe Ted was looking for a more efficient solution. > > 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. 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. Cheers robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/