From: Ted Flethuseo on 22 Jul 2010 00:21 Hi everyone, Is there a one line command to do this? I mean currently I use File.foreach(inFile) do |line| ... done I could do this with a counter, but that is SO UGLY, and I want to do an operation on that very last line. Regards, Ted. -- Posted via http://www.ruby-forum.com/.
From: prasad c on 22 Jul 2010 00:53 [Note: parts of this message were removed to make it a legal post.] last_line = '' IO.popen("tail -n 1 #{file_name}") { |f| last_line = f.gets } Regards, Prasad C On Thu, Jul 22, 2010 at 9:51 AM, Ted Flethuseo <flethuseo(a)gmail.com> wrote: > Hi everyone, > > Is there a one line command to do this? > > I mean currently I use > > File.foreach(inFile) do |line| > ... > done > > I could do this with a counter, but that is SO UGLY, and I want to do an > operation on that very last line. > > Regards, > Ted. > -- > Posted via http://www.ruby-forum.com/. > >
From: Urabe Shyouhei on 22 Jul 2010 01:08 Take a look at the doc for File.readline.
From: Urabe Shyouhei on 22 Jul 2010 01:10 (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
From: Robert Klemme on 22 Jul 2010 02:33 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 -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/
|
Next
|
Last
Pages: 1 2 3 Prev: Enumerators and generators Next: functions to escape and unescape strings in rails |