From: Robert Klemme Date: 2007-11-20T00:41:46+09:00 Subject: Re: Read last line of a file 2007/11/19, Gregory Seidman : > On Mon, Nov 19, 2007 at 11:38:08PM +0900, Shuaib Zahda wrote: > > Hi all > > > > I was trying to find a method to read only the last line of a file. I > > checked the the documentation and there is no such method. > > Beside reading line after line and checking the the eof method. Is there > > any faster or shorter way to do that. > > Depending on your platform, you might find that using popen with tail -1 > will be the fastest and easiest way. Failing that, consider this: > > last = File.open(myfile) { |f| f.extend(Enumerable).inject { |_,ln| ln } } You do not need the ".extend(Enumerable)" bit as an IO does already include Enumerable: $ ruby -e 'p Enumerable === $stdin' true Cheers robert -- use.inject do |as, often| as.you_can - without end