From: ES Date: 2005-08-24T08:26:30+09:00 Subject: Re: Pathname#walk for traversing path nodes (patch) Tanaka Akira wrote: > In article <43094510.6090406@magical-cat.org>, > ES writes: > > >>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. > > >>+ # Pathname.new('/path/to/some/file.rb').walk {|dir| p dir} >>+ # # >>+ # # >>+ # # >>+ # # > > > It seems that Pathname#walk doesn't yield the root directory. > Is it intended? How useful/unuseful with this behavior? > > Another question is about relative pathnames. > > % ruby -rpathname -e 'Pathname.new("a/b/c").walk {|x| p x}' > # > # > > The directory "a" doesn't appear in the result. > I think it is not intended. You are absolutely correct. How about this version? def walk() @path.scan(%r{[^/]*/?})[0...-1].inject('') do |path, dir| yield Pathname.new(path << dir) path end end # walk() E -- template void quack(duck& d) { d.quack(); }