# Dir.glob time of evaluation

**URL:** <https://www.ruby-forum.com/t/dir-glob-time-of-evaluation/91195>\
**Category:** Ruby\
**Created:** [April 5, 2007, 3:10pm UTC](https://www.ruby-forum.com/t/dir-glob-time-of-evaluation/91195 "2007-04-05T15:10:42Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [April 5, 2007, 3:10pm UTC](https://www.ruby-forum.com/t/dir-glob-time-of-evaluation/91195/1 "2007-04-05T15:10:42Z")

</div>

if i do :

Dir.glob("._xhtml"){|f|  
fo=f.gsub(/(._).xhtml$/,’\1\_orig.xhtml’)  
if   
`mv "#{f}" "#{fo}"`  
File.open("#{f}",‘w’).puts   
# 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{}

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

am i right or dreaming ???

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [April 5, 2007, 5:06pm UTC](https://www.ruby-forum.com/t/dir-glob-time-of-evaluation/91195/2 "2007-04-05T17:06:00Z")

</div>

On Thu, 5 Apr 2007, [ISO-8859-1] Une Bévue wrote:

> end  
> then i changed my script to :
> 
> fl=Dir.glob(".\*xhtml")  
> fl.each{}
> 
> 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

you should use

open(f, ‘w’){|fd| fd.puts }

to ensure that each file opened is also closed

regards.

-a

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [April 5, 2007, 7:55pm UTC](https://www.ruby-forum.com/t/dir-glob-time-of-evaluation/91195/3 "2007-04-05T19:55:19Z")

</div>

[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > am i right or dreaming ???
> 
> 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
> 
> > File.open(“#{f}”,‘w’).puts
> 
> you should use
> 
> open(f, ‘w’){|fd| fd.puts }
> 
> to ensure that each file opened is also closed

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

> regards.

best,
