Feature #3608

Enhancing Pathname#each_child to be lazy

Added by taw (Tomasz Wegrzanowski) about 10 years ago. Updated almost 3 years ago.

Target version:


Right now it lists entire directory, then yields
every element, that is x.each_child(&b) means x.children.each(&b).

This is too slow for directories mounted over networked file systems etc.,
and there is currently no way to get lazy behaviour, other than leaving
convenient #each_child/#children API and moving to lower level.

With this patch:

  • #children is eager like before, no change here
  • #each_child becomes lazy
  • #each_child without block returns lazy enumerator, so it can be used like this dir.each_child.find(&:symlink?) without losing laziness.

Patch is against trunk. pathname.rb was in lib/ not ext/pathname/lib/
before, but it works either way.

The part to return enumerator when called without a block wouldn't
work in 1.8. If backport is desired, that line would need to be thrown
away, and #children would need to build result array instead
of calling each_child(with_directory).to_a - this would be straightforward.


lazy_each_child.diff (1.19 KB) lazy_each_child.diff taw (Tomasz Wegrzanowski), 07/24/2010 10:27 AM
lazy_path_test.rb (1.06 KB) lazy_path_test.rb taw (Tomasz Wegrzanowski), 08/02/2010 05:43 AM

Also available in: Atom PDF