while !file.eof?

D

Derek Smith

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
 
B

botp

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|
=A0 =A0 =A0 =A0while !mailog.eof?
=A0 =A0 =A0 =A0 =A0 =A0if line =3D~ /^#{last_record}/o
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0p line
=A0 =A0 =A0 =A0 =A0 =A0end
=A0 =A0 =A0 =A0end
}


try eg,

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

kind regards
-botp
 
J

James Gray

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

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

James Edward Gray II
 
R

Robert Klemme

2009/8/17 botp said:
on your case, you do not need the EOF sig. Pls read on ruby's
iterators and File.



try eg,

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

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

Better:

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

Kind regards

robert

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

Derek Smith

Robert said:
You're not closing the file handle properly. :)

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.
 
K

Kyle Schmitt

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 .+/)
 
R

Robert Klemme

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.

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
 
G

g_f

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? =A0I did not see a diff.
Personally I like "without the do" as there is less to type.

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
 
B

botp

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

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

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
 
R

Robert Klemme

2009/8/18 botp said:
i was leaning on the op's example... :) =A0For the record, when it comes
to file handling, i do not even do shortcuts ;)
:)

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

=A0 =A0File.open(foo).each do
=A0 =A0 =A0 blah
=A0 =A0end

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.
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 alway=
s do

class File
class <<self
alias better_name foreach
end
end

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

Kind regards

robert


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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

A regexp? 2
While Loop Freezing? 1
C program: memory leak/ segmentation fault/ memory limit exceeded 0
Issue with textbox script? 0
MDP Modeling in GAMS 1
Help with code 0
Minimum Total Difficulty 0
Text processing 29

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top