While !file.eof?

How can I use a while loop in such as way where I want to process the
file until EOF? thank you!

Here is my code:

mailog="/tmp/maillog"
last_record="Jul 15 22:09:10"
File.open(mailog, 'r+').each { |line|
        while !mailog.eof?
            if line =~ /^#{last_record}/o
                p line
            end
        end
}

mail.scr:6: undefined method `eof?' for "/tmp/maillog":String
(NoMethodError)
  from mail.scr:5:in `each'
  from mail.scr:5

···

--
Posted via http://www.ruby-forum.com/.

How can I use a while loop in such as way where I want to process the
file until EOF?

on your case, you do not need the EOF sig. Pls read on ruby's
iterators and File.

....
File.open(mailog, 'r+').each { |line|
while !mailog.eof?
if line =~ /^#{last_record}/o
p line
end
end
}

try eg,

File.open( "maillog" ).each do |line|
  p line if line =~ /^#{last_record}/
end

kind regards
-botp

···

On Mon, Aug 17, 2009 at 10:03 AM, Derek Smith<derekbellnersmith@yahoo.com> wrote:

each() is already doing that in the code you showed, so the easiest way is to just remove the entire while loop. :slight_smile:

James Edward Gray II

···

On Aug 16, 2009, at 9:03 PM, Derek Smith wrote:

How can I use a while loop in such as way where I want to process the
file until EOF? thank you!

It's not always appropriate, but File.read() is pretty slick too...
I'm pretty sure it's just syntactic sugar, but it makes things quite clear.

data=File.read("somefile")

If you want to just get content specific lines for instance, you'd use
scan on top of that.

may_lines=File.read("/var/log/messages").scan(/May .+/)

You're not closing the file handle properly. :slight_smile:

Better:

File.foreach "maillog" do |line|
  p line if /^#{last_record}/o =~ line
end

Kind regards

robert

···

2009/8/17 botp <botpena@gmail.com>:

On Mon, Aug 17, 2009 at 10:03 AM, Derek > Smith<derekbellnersmith@yahoo.com> wrote:

How can I use a while loop in such as way where I want to process the
file until EOF?

on your case, you do not need the EOF sig. Pls read on ruby's
iterators and File.

....
File.open(mailog, 'r+').each { |line|
while !mailog.eof?
if line =~ /^#{last_record}/o
p line
end
end
}

try eg,

File.open( "maillog" ).each do |line|
p line if line =~ /^#{last_record}/
end

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

Robert Klemme wrote:

� � � �while !mailog.eof?

�p line if line =~ /^#{last_record}/
end

You're not closing the file handle properly. :slight_smile:

Better:

File.foreach "maillog" do |line|
  p line if /^#{last_record}/o =~ line
end

Kind regards

robert

OK thx guys!

mailog="/tmp/maillog"
last_record="Jul 15 22:09:10"
File.open(mailog, 'r+').each { |line|
     p line if line =~ /^#{last_record}/o
}

But now, what is the diff between

File.open( "maillog" ).each do |line|
and
File.open("mailog").each { |line|

one better, or more ruby standard? I did not see a diff.
Personally I like "without the do" as there is less to type.

···

2009/8/17 botp <botpena@gmail.com>:

--
Posted via http://www.ruby-forum.com/\.

You're not closing the file handle properly. :slight_smile:

i was leaning on the op's example... :slight_smile: For the record, when it comes
to file handling, i do not even do shortcuts :wink:

i think i have a question for you, cool ruby hacker robert:
if i do something like,

    File.open(foo).each do
       blah
    end

will ruby be able to cleanup the orphaned file handle?

Better:

File.foreach "maillog" do |line|

i am not a fan of foreach because i do not like the method name as
used... it's like saying "for each file"??
but that is just me...

best regards
-botp

···

On Mon, Aug 17, 2009 at 2:50 PM, Robert Klemme<shortcutter@googlemail.com> wrote:

The difference is just precedence. But actually both are bad idioms as they do not ensure that the File object is closed properly. File.foreach is better (see above).

For a bit more background you can read on here

http://blog.rubybestpractices.com/posts/rklemme/002_Writing_Block_Methods.html

Kind regards

  robert

···

On 17.08.2009 17:39, Derek Smith wrote:

Robert Klemme wrote:

2009/8/17 botp <botpena@gmail.com>:

You're not closing the file handle properly. :slight_smile:

Better:

File.foreach "maillog" do |line|
  p line if /^#{last_record}/o =~ line
end

OK thx guys!

mailog="/tmp/maillog"
last_record="Jul 15 22:09:10"
File.open(mailog, 'r+').each { |line|
     p line if line =~ /^#{last_record}/o
}

But now, what is the diff between

File.open( "maillog" ).each do |line|
and
File.open("mailog").each { |line|

one better, or more ruby standard? I did not see a diff.
Personally I like "without the do" as there is less to type.

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

Ah. "do ... end" vs. "{ ... }". Yes, that was a bit confusing at
first.

They're basically the same - they signify a block being passed into
the method.

The curly-brace "{ ... }" block is generally used when you're using a
single-line of code and "do ... end" is generally a multi-line block.

Also, I always use "{ ... }" for map/collect or select/reject methods
even if they're taking multiple lines, but that's because I wrote in
Perl for ages and that's how it did it. I find that the curly-braces
stand out more and for skanky code I want to make it easy to see where
the block starts and ends. "do" and "end" tend to blend into the rest
of the code making it "prettier" because it's all text and not looking
like line-noise.

Greg

···

On Aug 17, 8:39 am, Derek Smith <derekbellnersm...@yahoo.com> wrote:

But now, what is the diff between

File.open( "maillog" ).each do |line|
and
File.open("mailog").each { |line|

one better, or more ruby standard? I did not see a diff.
Personally I like "without the do" as there is less to type.

You're not closing the file handle properly. :slight_smile:

i was leaning on the op's example... :slight_smile: For the record, when it comes
to file handling, i do not even do shortcuts :wink:

:slight_smile:

i think i have a question for you, cool ruby hacker robert:
if i do something like,

File.open(foo).each do
blah
end

will ruby be able to cleanup the orphaned file handle?

Probably yes. But: it will certainly be later than necessary (when a
finalizer is run which may be even as late as process exit). Plus, if
you open the file again in the same interpreter instance you may face
strange effects. That may be not so important for files which are
read only, but it can create serious issues when writing files.

My stance is this: release resources as soon as possible, especially
if it is as easy as with files.

Better:

File.foreach "maillog" do |line|

i am not a fan of foreach because i do not like the method name as
used... it's like saying "for each file"??
but that is just me...

I would aesthetics not come in my way that far. But even if: you can always do

class File
  class <<self
    alias better_name foreach
  end
end

File.better_name "foo" do |line|
  ...
end

Kind regards

robert

···

2009/8/18 botp <botpena@gmail.com>:

On Mon, Aug 17, 2009 at 2:50 PM, Robert > Klemme<shortcutter@googlemail.com> wrote:

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/