From: ES Date: 2005-08-22T12:02:51+09:00 Subject: Pathname#walk for traversing path nodes (patch) Here is a small addition to Pathname against 1.9, probably suited for 1.8. Makes doing something for each node in a path easier. Index: pathname.rb =================================================================== RCS file: /src/ruby/lib/pathname.rb,v retrieving revision 1.42 diff -u -r1.42 pathname.rb --- pathname.rb 10 Jul 2005 15:17:18 -0000 1.42 +++ pathname.rb 22 Aug 2005 02:58:46 -0000 @@ -423,6 +423,23 @@ end # + # Iterates over and yields a new Pathname object + # for each element in the given path. + # + # Pathname.new('/path/to/some/file.rb').walk {|dir| p dir} + # # + # # + # # + # # + # + def walk() + @path.scan(%r{/[^/]+}).inject('') do |path, dir| + yield Pathname.new(path << dir) + path + end + end # walk() + + # # Pathname#+ appends a pathname fragment to this one to produce a new Pathname # object. # E -- template void quack(duck& d) { d.quack(); }