Loop behavior not understood (newbie)

T

Thomas Luedeke

My coding looks like this:

===================================================================

detailedModel = File.open( "detailed_model", "r" )
detailedModelModified = File.open( "detailed_model_mod", "w" )
string = nil
flag = false
detailedModel.each_line do |line|
if (flag == false) || (line.lstrip =~ /^[\*]/) then
detailedModelModified.puts line
if (line.lstrip =~ /^(41)/) then
flag = true
next
end
end
end

===================================================================

When running Ruby 1.8.7 on Eclipse, this results in nothing being
printed to the detailed_model_mod file. If I take out the if-end loop
trying to pattern match the string with the leading "41", then it prints
out fine.

What in the heck is going on here? I've tried a number of different
things, and fail to understand this behavior.

Respectfully, TPL
 
G

Gregory Brown

When running Ruby 1.8.7 on Eclipse, this results in nothing being
printed to the detailed_model_mod file. If I take out the if-end loop
trying to pattern match the string with the leading "41", then it prints
out fine.

Does this code give you what you want?

###

check_disabled = false

File.open( "detailed_model_mod", "w" ) do |mod|
File.foreach("detailed_model") do |line|
mod.puts(line) if check_disabled || line =~ /^\s*[\*]/
check_disabled = true if line =~ /^\s*41/
end
end

###

I *think* it does the same thing as yours, but it is sure to close the
file handles cleanly. When I ran your code however, I was able to get
some output.

If this isn't what you want, please provide a sample data set and show
what you expect the output to be.

-greg
 
T

Thomas Luedeke

Gregory said:
When running Ruby 1.8.7 on Eclipse, this results in nothing being
printed to the detailed_model_mod file. If I take out the if-end loop
trying to pattern match the string with the leading "41", then it prints
out fine.

Does this code give you what you want?

###

check_disabled = false

File.open( "detailed_model_mod", "w" ) do |mod|
File.foreach("detailed_model") do |line|
mod.puts(line) if check_disabled || line =~ /^\s*[\*]/
check_disabled = true if line =~ /^\s*41/
end
end

###

I *think* it does the same thing as yours, but it is sure to close the
file handles cleanly. When I ran your code however, I was able to get
some output.

If this isn't what you want, please provide a sample data set and show
what you expect the output to be.

-greg


If I change "detailedModelModified.puts line" to "puts line" in my
coding, it works as intended. The problem is in the print to the
detailedModelModified file.

I'll try your version and see if it works in my coding. Be back in a
minute.
 
G

Gregory Brown

If I change "detailedModelModified.puts line" to "puts line" in my
coding, it works as intended. The problem is in the print to the
detailedModelModified file.

I'll try your version and see if it works in my coding. Be back in a
minute.

Actually, I didn't notice your nested ifs. This should be equivalent

check_disabled = false

File.open( "detailed_model_mod", "w" ) do |mod|
File.foreach("detailed_model") do |line|
if check_disabled || line =~ /^\s*[\*]/
mod.puts(line)
check_disabled = true if line =~ /^\s*41/
end
end
end
 
T

Thomas Luedeke

Gregory said:
If I change "detailedModelModified.puts line" to "puts line" in my
coding, it works as intended. The problem is in the print to the
detailedModelModified file.

I'll try your version and see if it works in my coding. Be back in a
minute.

Actually, I didn't notice your nested ifs. This should be equivalent

check_disabled = false

File.open( "detailed_model_mod", "w" ) do |mod|
File.foreach("detailed_model") do |line|
if check_disabled || line =~ /^\s*[\*]/
mod.puts(line)
check_disabled = true if line =~ /^\s*41/
end
end
end


OK, your version seems to work. I'm confused, however, because I don't
see the functional difference between your coding and mine. What was
the error in my coding previously?? Why did it not know how to print to
the intended file??
 
G

Gregory Brown

Gregory Brown wrote:
OK, your version seems to work. I'm confused, however, because I don't
see the functional difference between your coding and mine. What was
the error in my coding previously?? Why did it not know how to print to
the intended file??

The primary functional difference is that when you use the block form
of File.open(), it closes the file handle for you. Your code was not
properly closing the files.
It still wrote out a file on my machine, but running under eclipse may
be different.

If you'd like to try to verify that, add these two lines to the end of
your file:

detailedModel.close
detailedModelModified.close

The rest was just making the code more conventional so that others
will have an easier time reading it.
If you're interested in those, here's what I did:

1. named variables like_this instead of likeThis.

2. used the block form of File to avoid having to manually call File#close

3. Avoided comparing to a boolean. if (something) is the same as if
(something == false), unless you want to prevent nil objects from
evaluating as false.
Everything in Ruby except false and nil are true.

4. Removed the unnecessary 'then' syntax.

5. Shortened the skip line by using the conditional modifier:
do_something if some_condition

Although it's of course not necessary to write things this way, it is
more idiomatic, which makes it easier for people on this list to read
it and help you.

Try closing the files in the code you provided, and see if that works.
If it doesn't, I feel it's a bit of an Eclipse mystery.

-greg
 
G

Gregory Brown

3. Avoided comparing to a boolean. if (something) is the same as if
(something == false), unless you want to prevent nil objects from
evaluating as false.

Whoops.

Here I mean:

something=false

then

if(something) is the same as if(something == false)
 
G

Gregory Brown

Whoops.

Here I mean:

something=false

then

if(something) is the same as if(something == false)

Sorry, I totally have confused myself (but hopefully not you).

What you're looking for when you do

if(something == false)

is either

unless(something)

or

if(!something)

My point was that you don't need to explicitly check against a
boolean, but then I made things much more confusing. Sorry for the
triple post!

-greg
 
T

Thomas Luedeke

Gregory said:
Sorry, I totally have confused myself (but hopefully not you).

What you're looking for when you do

if(something == false)

is either

unless(something)

or

if(!something)

My point was that you don't need to explicitly check against a
boolean, but then I made things much more confusing. Sorry for the
triple post!

-greg


Actually, your explanation of the loop closing the file handle makes a
lot of sense regarding what I was seeing. I also did not know Ruby did
that.

In fact, in my newbie meanderings, I seem to recall that if I put a
File.close outside of a loop just like that one, it always spawned an
error. No wonder.

Thanks so much for your help!! I'll have to dig out my Ruby book and
read up on file handles.
 
S

Shadowfirebird

Maybe your original code was writing to the file, but only a few
characters; and because the file handle was never closed, the buffer
was not flushed and the output written?

It's happened to me in other languages. I don't know whether Ruby
would behave like that.


Actually, your explanation of the loop closing the file handle makes a
lot of sense regarding what I was seeing. I also did not know Ruby did
that.

In fact, in my newbie meanderings, I seem to recall that if I put a
File.close outside of a loop just like that one, it always spawned an
error. No wonder.

Thanks so much for your help!! I'll have to dig out my Ruby book and
read up on file handles.



--
Me, I imagine places that I have never seen / The colored lights in
fountains, blue and green / And I imagine places that I will never go
/ Behind these clouds that hang here dark and low
But it's there when I'm holding you / There when I'm sleeping too /
There when there's nothing left of me / Hanging out behind the
burned-out factories / Out of reach but leading me / Into the
beautiful sea
 

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

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top