Problem with null field. Plz help!

K

Kurt Euler

Hi all.

Could someone please help me diagnose what might be wrong with this code snippet (taken from within a program) that cropped up after I upgraded from Ruby 1.68 to 1.8. The problem is that the Ruby crashes with this message:

autogen.rb:301:in `initialize': Permission denied - ./boiler_install_notes/ (Errno::EACCES)
from autogen.rb:301:in `open'
from autogen.rb:301
from autogen.rb:92:in `foreach'
from autogen.rb:92
Exit code: 1

What's weird is that the crash happens when field[5] should be nil -- the entire if/end should be skipped. (If I insert a line # 297 puts field[5], console just shows does a line space as all puts do. No space or tab appears there.)


298 if field[5] != ""
299 if FileTest.exist?("./boiler_install_notes/#{field[5]}" )
300 install_note = ""
301 File.open("./boiler_install_notes/#{field[5]}") { |f| install_note = f.read.strip }
302 content2.gsub!(/<start_note_2>/, install_note)
303 puts "start_note_2: #{field[5]} added."
304 outf.puts "start_note_2: #{field[5]} added."
305 else
306 # Warn if note file specified in field[5] doesn't exist.
307 puts "start_note_2: #{field[5]} doesn't exist"
308 outf.puts "start_note_2: #{field[5]} doesn't exist"
309 end
310 end


Thanks!

Kurt Euler
 
T

ts

K> autogen.rb:301:in `initialize': Permission denied - ./boiler_install_notes/ (Errno::EACCES)

One possible case is

svg% ls -ld aa
d--------- 2 ts ts 4096 Aug 23 13:54 aa/
svg%

svg% ruby -e 'p FileTest.exist?("aa"); File.open("aa")'
true
-e:1:in `initialize': Permission denied - aa (Errno::EACCES)
from -e:1:in `open'
from -e:1
svg%

K> What's weird is that the crash happens when field[5] should be nil --
K> the entire if/end should be skipped. (If I insert a line # 297 puts
K> field[5], console just shows does a line space as all puts do. No space
K> or tab appears there.)

The problem is that you don't make the right test, normally it's

K> 298 if field[5] != ""

if field[5].to_s != ""

K> 299 if FileTest.exist?("./boiler_install_notes/#{field[5]}" )

because this is what do ruby when you write "#{field[5]}" (i.e. it call
#to_s for field[5])


Guy Decoux
 

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

No members online now.

Forum statistics

Threads
474,109
Messages
2,570,671
Members
47,262
Latest member
EffiePju4

Latest Threads

Top