| Message: 4755 |
 |
BY: Leon Barrett (phaedrus) DATE: 2005-08-18 19:43 SUBJECT: RE: I found it! That looks good, but it still uses a defined function instead of just a block. Here's how I would do it with just a block. (Notice that func is not declared anywhere.)
#
# recursive walk that lets you pass in
# a function, like Python's os.path.walk
#
DIR = "/home/ruby/Mail" # some dir tree to walk
def walk(top, &block)
names = Dir.entries(top)
names.delete(".")
names.delete("..")
yield(top, names)
names.each{ | name |
name = File.join(top, name)
if File.ftype(name) == "directory"
walk(name,&block) #recurse, passing the same block
end
}
end
walk(DIR) { |*list|
print "dir: "+list[0]+"\n"
list[1].each{ |x|print x + "\n"}
}
| |