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
On Wed, Jul 30, 2008 at 2:59 PM, Thomas L.
[email protected] wrote:
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
Gregory B. wrote:
On Wed, Jul 30, 2008 at 2:59 PM, Thomas L.
[email protected] wrote:
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.
Gregory B. wrote:
On Wed, Jul 30, 2008 at 3:33 PM, Thomas L.
[email protected] wrote:
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??
On Wed, Jul 30, 2008 at 3:33 PM, Thomas L.
[email protected] wrote:
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
On Wed, Jul 30, 2008 at 4:17 PM, Gregory B.
[email protected] wrote:
- 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)
On Wed, Jul 30, 2008 at 4:07 PM, Thomas L.
[email protected] wrote:
Gregory B. 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:
-
named variables like_this instead of likeThis.
-
used the block form of File to avoid having to manually call
File#close
-
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.
-
Removed the unnecessary ‘then’ syntax.
-
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
On Wed, Jul 30, 2008 at 4:19 PM, Gregory B.
[email protected] wrote:
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
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.
On Wed, Jul 30, 2008 at 9:29 PM, Thomas L.
[email protected] wrote:
Sorry, I totally have confused myself (but hopefully not you).
lot of sense regarding what I was seeing. I also did not know Ruby did
–
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
Gregory B. wrote:
On Wed, Jul 30, 2008 at 4:19 PM, Gregory B.
[email protected] wrote:
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
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.