Feature #8269

stdlib: Add Find.each_file to accompany Find.find

Added by Thomas Green about 1 year ago. Updated 11 months ago.

[ruby-core:54284]
Status:Feedback
Priority:Normal
Assignee:-
Category:lib
Target version:-

Description

Often when I use the find module, I am only interested in files.
Since Find.find returns files and directories, I often write code
that looks like this:

Find.find('foo/bar') do |f|
next if File.directory? f

code code code ...
end

To remove the test for directories, a class method, called find.each_file
could be added, which would only yield files.

module Find
def self.eachfile(*paths)
block
given? or return enumfor(method_, paths)
self.find(
paths) {|f yield f unless File.directory? f}
end
end

8269_Find_each_file.patch Magnifier (1.81 KB) Matthew Kerwin, 04/22/2013 03:06 PM

History

#1 Updated by Matthew Kerwin 12 months ago

Is there a use-case for each_directory as well?

(Attached patch is only for each_file method, and includes basic tests)

#2 Updated by Zachary Scott 11 months ago

  • Category set to lib
  • Status changed from Open to Feedback

I think this is out of the scope of find.rb, see "find.rb: the Find module for processing all files under a given directory."

If you want this feature, I think you should write a gem.

Also available in: Atom PDF