Forums | Admin

Discussion Forums: rubys_taqueria_espiritu

Start New Thread Start New Thread
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"}
}



Thread View

Thread Author Date
Help Ruby do good RubyRuby Dos Zapatas2005-08-02 22:48
      RE: Help Ruby do good RubyLeon Barrett2005-08-17 05:03
            RE: Help Ruby do good RubyLeon Barrett2005-08-17 05:15
                  RE: defining a blockRuby Dos Zapatas2005-08-17 15:19
                        RE: defining a blockLeon Barrett2005-08-17 16:45
                              I found it!Ruby Dos Zapatas2005-08-18 15:32
                                    RE: I found it!Leon Barrett2005-08-18 19:43
                                          You are rightRuby Dos Zapatas2005-08-20 14:12

Post a followup to this message