[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Why does Dir#glob("**/*") miss dot directories?

timr

2/27/2009 9:32:00 AM

I just noticed the Unix find command is a bit more thorough than
Dir.glob. The difference is that find looks in hidden directories
(.directories), while Dir.glob doesn't. Here is a demonstration:

Tim:~/Desktop/test> ls -la
total 0
drwxr-xr-x 3 Tim staff 102 Feb 27 01:07 .
drwx------+ 154 Tim staff 5236 Feb 27 01:06 ..
drwxr-xr-x 2 Tim staff 68 Feb 27 01:06 .gem
Tim:~/Desktop/test> cd .gem
Tim:~/Desktop/test/.gem> cat > findme.rb
Tim:~/Desktop/test/.gem> ls
findme.rb
Tim:~/Desktop/test/.gem> cd ..
Tim:~/Desktop/test> find . -name '*.rb' -print
../.gem/findme.rb
Tim:~/Desktop/test> ruby -e 'Dir.glob("**/*.rb")'
Tim:~/Desktop/test>

So is there a way to get glob to additionally look inside
of .directories?
Alternatively, if you know of another pure and elegant way of finding
all files recursively, even in dot directories perhaps with File or
something, I would greatly appreciate an example.
Thanks,
Tim
2 Answers

Markus Prinz

2/27/2009 9:46:00 AM

0


On 27.02.2009, at 10:34, timr wrote:
> So is there a way to get glob to additionally look inside
> of .directories?

Yes, by specifying File::FNM_DOTMATCH as the second argument:

ruby -e 'Dir.glob("**/*.rb", ile::FNM_DOTMATCH)'

> Alternatively, if you know of another pure and elegant way of finding
> all files recursively, even in dot directories perhaps with File or
> something, I would greatly appreciate an example.

Maybe this is what you're looking for?

From the Find documentation (http://www.ruby-doc.org/stdlib/libdoc/find/rdoc/...
):
require 'find'

total_size = 0

Find.find(ENV["HOME"]) do |path|
if FileTest.directory?(path)
if File.basename(path)[0] == ?.
Find.prune # Don't look any further into this directory.
else
next
end
else
total_size += FileTest.size(path)
end
end
g, Markus

timr

2/27/2009 4:17:00 PM

0

Hi Markus, Thanks for the info. Both strategies work great.

Tim:~/Desktop/test> ruby -e 'puts Dir.glob("**/*.rb",
File::FNM_DOTMATCH)'
..gem/.find_hidden_me.rb
..gem/findme.rb


Here is the irb session from the second idea to demonstrate.

>> Dir.chdir("/Users/Tim/Desktop/test")
=> 0
>> Dir.pwd
=> "/Users/Tim/Desktop/test"
>> Find.find("."){|x| p x if File.file?(x)}
"./.gem/findme.rb"
=> nil
>>

Thanks,
Tim





On Feb 27, 1:46 am, Markus Prinz <markus.pr...@nuclearsquid.com>
wrote:
> On 27.02.2009, at 10:34, timr wrote:
>
> > So is there a way to get glob to additionally look inside
> > of .directories?
>
> Yes, by specifying File::FNM_DOTMATCH as the second argument:
>
> ruby -e 'Dir.glob("**/*.rb", ile::FNM_DOTMATCH)'
>
> > Alternatively, if you know of another pure and elegant way of finding
> > all files recursively, even in dot directories perhaps with File or
> > something, I would greatly appreciate an example.
>
> Maybe this is what you're looking for?
>
>  From the Find documentation (http://www.ruby-doc.org/stdlib/libdoc/find/rdoc/...
> ):
> require 'find'
>
> total_size = 0
>
> Find.find(ENV["HOME"]) do |path|
>    if FileTest.directory?(path)
>      if File.basename(path)[0] == ?.
>        Find.prune       # Don't look any further into this directory.
>      else
>        next
>      end
>    else
>      total_size += FileTest.size(path)
>    end
> end
> g, Markus