Dir.glob time of evaluation

U

Une Bévue

if i do :

Dir.glob(".*xhtml"){|f|
fo=f.gsub(/(.*)\.xhtml$/,'\1_orig.xhtml')
if <the file has to be updated>
`mv "#{f}" "#{fo}"`
File.open("#{f}",'w').puts <something updated>
# c is true when we want to clean up orig files
`rm -f "#{fo}"` if c
end
}

i got an error like :
/Users/yt/bin/set_xhtml_date:58:in `ftype': No such file or directory -
/Users/yt/Sites/Events/event_listener_orig.xhtml (Errno::ENOENT)

i think this is because Dir.glob "sees" the saved file fo
(/Users/yt/Sites/Events/event_listener_orig.xhtml in this case)
before it has been removed with "`rm -f "#{fo}"`"

then i changed my script to :

fl=Dir.glob(".*xhtml")
fl.each{<same as before>}

then, because fl is evaluated before any added *_orig.xhtml file i don't
get anymore this error...

am i right or dreaming ????
 
A

ara.t.howard

if i do :

Dir.glob(".*xhtml"){|f|
fo=3Df.gsub(/(.*)\.xhtml$/,'\1_orig.xhtml')
if <the file has to be updated>
`mv "#{f}" "#{fo}"`
File.open("#{f}",'w').puts <something updated>
# c is true when we want to clean up orig files
`rm -f "#{fo}"` if c
end
}

i got an error like :
/Users/yt/bin/set_xhtml_date:58:in `ftype': No such file or directory -
/Users/yt/Sites/Events/event_listener_orig.xhtml (Errno::ENOENT)

i think this is because Dir.glob "sees" the saved file fo
(/Users/yt/Sites/Events/event_listener_orig.xhtml in this case)
before it has been removed with "`rm -f "#{fo}"`"

then i changed my script to :

fl=3DDir.glob(".*xhtml")
fl.each{<same as before>}

then, because fl is evaluated before any added *_orig.xhtml file i don't
get anymore this error...

am i right or dreaming ????

you are not dreaming.

you're also crazy for shelling out to do the moving and removing:

require 'fileutils'

FileUtils.mv f, fo
FileUtils.rm_rf fo if c

also, you are leaving open file handles all over the place with this line
File.open("#{f}",'w').puts <something updated>

you should use

open(f, 'w'){|fd| fd.puts <something updated>}

to ensure that each file opened is also closed

regards.

-a
--=20
be kind whenever possible... it is always possible.
- the dalai lama
 
U

Une Bévue

you are not dreaming.

fine thanks a lot !
you're also crazy for shelling out to do the moving and removing:

require 'fileutils'

FileUtils.mv f, fo
FileUtils.rm_rf fo if c

oh, i see may be the prob comes only from my shelling ?
I'll change asap this bad habit coming from zsh ))
also, you are leaving open file handles all over the place with this line


you should use

open(f, 'w'){|fd| fd.puts <something updated>}

to ensure that each file opened is also closed

i thought, wrongly i presume, File.open() did also the closing )))

best,
 

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,780
Messages
2,569,611
Members
45,268
Latest member
AshliMacin

Latest Threads

Top