From: Gregory Seidman Date: 2007-11-20T03:08:23+09:00 Subject: Re: Read last line of a file On Tue, Nov 20, 2007 at 12:41:46AM +0900, Robert Klemme wrote: > 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 Ah, sorry, you are correct. For some reason I was thinking of MySQL result sets. > Cheers > robert --Greg