From: Ted Flethuseo on
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
[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
Take a look at the doc for File.readline.


From: Urabe Shyouhei on
(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
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/