ruby picture

RCR 151: Dir.recurse

Submitted by anonymous (Thu Aug 21 14:08:47 UTC 2003)

Abstract

This is a legacy RCR from Ruby Garden, submitted by anonymous. Matz has declared these RCRs obsolete, and asked that their authors resubmit them in the new format.

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

Problem

(RCR imported from old format)

Proposal

Analysis

(RCR imported from old format)

Implementation

(RCR imported from old format)
ruby picture
Comments Current voting

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.


Strongly opposed 0
Opposed 0
Neutral 0
In favor 0
Strongly advocate 0
ruby picture
If you have registered at RCRchive, you may now sign in below. If you have not registered, you may sign up for a username and password. Registering enables you to submit new RCRs, and vote and leave comments on existing RCRs.
Your username:
Your password:

ruby picture

Powered by .