From: Ted Flethuseo Date: 2010-07-22T23:08:34+09:00 Subject: Re: finding last line in a file 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. > > Ted, if you want to do this in Ruby and not resort to tail you can use > IO#seek to seek to the end of the file and read backwards until you have > read more than one line. > > Kind regards > > robert Interesting, the problem is how to find how far back I need to go in the seek (automatically of course) f = File.new("testfile") f.seek(-13, IO::SEEK_END) #=> 0 f.readline #=> "And so on...\n" Ted. -- Posted via http://www.ruby-forum.com/.