From: Urabe Shyouhei Date: 2008-12-17T00:26:43+09:00 Subject: Re: implementing python's os.walk Brad Volz wrote: > Hello, > > I seem to be having some difficulty creating a version of python's > os.walk() for ruby, and I was hoping for some pointers. You should really know how to use Ruby's stdlib named "find". http://www.ruby-doc.org/stdlib/libdoc/find/rdoc/index.html Below is another twisted example in Ruby 1.9. It can be used much like python's generators: irb(main):060:0> for root, dirs, files in os_walk("/dev/disk") do irb(main):061:1* puts root irb(main):062:1> end require 'find' require 'pathname' def os_walk(dir) ret = Fiber.new do Pathname(dir).find do |ent| next unless ent.directory? dirs, files = ent.children.partition {|i| i.directory? } Fiber.yield ent, dirs, files end raise end def ret.each loop { yield resume } rescue self end ret end