Comment on this RCR (edit wiki page) | RCRchive home

RCR 151: Dir.recurse

submitted by anonymous on 2003-08-21 10:57:47


This is a legacy RCR. If this is your RCR, please resubmit it using the new format and process.


Simple enough, a "recurse" method for class Dir. This would take a block, and call the block for every subdirectory of the given (or current, in the absence of an argument) directory. Optionally, it could take a "max depth" argument. The absolute path of each directory recursed into would be passed into the block. So, given a directory setup like
       /dir
      /    
  subdir   subdir2
   /   
subsub  subsub2
Doing
Dir.recurse(".") { |d| puts d}
Would print
/dir
/dir/subdir
/dir/subdir2
/dir/subdir/subsub
/dir/subdir/subsub2
With a maxdepth argument,
Dir.recurse("/dir", 1) {|d| puts d}
would print
/dir
/dir/subdir
/dir/subdir2

Vote for this RCR

Strongly opposed [0]
Opposed [4]
Neutral [0]
In favor [3]
Strongly advocate [1]

Change the status of this RCR to:

accepted

rejected

withdrawn


Add comments here

Probably not needed (, 2003-08-25 06:41:06)

I posted this RCR. I posted it again, along with a rough implementation, to comp.lang.ruby, where it was pointed out to me that Find.find can do much the same thing.

The only major differences are that Find.find "finds" everything, not just directories, and doesn't have an argument to specify the max depth to which to recurse. The first is probably not an issue at all, because you would normally be more interested in the files within directories than in the directories themselves. In case it's the directories that are interesting, it's easy enough to insert a "next unless FileTest.directory? f" or the like in the block. The "max depth" argument could still be useful, I think, but it can likewise be pretty easily implemented within the block passed to Find.find.

In any case, I don't think this RCR has any merit as-is, because the proper place to implement anything extra would be within Find.find, of which I was not originally aware.

Dir can do something like that... (flori, 2003-09-08 06:34:31)

if you use this special globbing feature:

Dir["/etc/**/*"].each do |x| puts x end

lists all files in /etc and its recursive sub directories.

Back to RCRchive.


RCR Submission page and RCRchive powered by Ruby, Apache, RuWiki (modified), and RubLog