Skip to content

Commit e49a5cf

Browse files
[DOC] Doc for Find (#24)
1 parent 1ec6f82 commit e49a5cf

1 file changed

Lines changed: 52 additions & 30 deletions

File tree

lib/find.rb

Lines changed: 52 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,41 +3,50 @@
33
# find.rb: the Find module for processing all files under a given directory.
44
#
55

6+
# :markup: markdown
67
#
7-
# The +Find+ module supports the top-down traversal of a set of file paths.
8-
#
9-
# For example, to total the size of all files under your home directory,
10-
# ignoring anything in a "dot" directory (e.g. $HOME/.ssh):
11-
#
12-
# require 'find'
13-
#
14-
# total_size = 0
15-
#
16-
# Find.find(ENV["HOME"]) do |path|
17-
# if FileTest.directory?(path)
18-
# if File.basename(path).start_with?('.')
19-
# Find.prune # Don't look any further into this directory.
20-
# else
21-
# next
22-
# end
23-
# else
24-
# total_size += FileTest.size(path)
25-
# end
26-
# end
27-
#
8+
# \Module \Find supports the top-down traversal of entries in the file system.
289
module Find
2910

3011
# The version string
3112
VERSION = "0.2.0"
3213

14+
# :markup: markdown
3315
#
34-
# Calls the associated block with the name of every file and directory listed
35-
# as arguments, then recursively on their subdirectories, and so on.
16+
# With a block given, performs a depth-first traversal of each given path in `paths`;
17+
# calls the block with each found file or directory path:
3618
#
37-
# Returns an enumerator if no block is given.
19+
# ```ruby
20+
# paths = []
21+
# Find.find('bin', 'jit') {|path| paths << path }
22+
# paths
23+
# # =>
24+
# # ["bin",
25+
# # "bin/gem",
26+
# # "jit",
27+
# # "jit/Cargo.toml",
28+
# # "jit/src",
29+
# # "jit/src/lib.rs"]
30+
# ```
3831
#
39-
# See the +Find+ module documentation for an example.
32+
# Raises an exception if a given path cannot be read.
4033
#
34+
# When keyword argument `ignore_error` is given as `true` (the default),
35+
# certain exceptions during traversal are ignored (i.e., silently rescued):
36+
# Errno::ENOENT, Errno::EACCES, Errno::ENOTDIR, Errno::ELOOP, Errno::ENAMETOOLONG, Errno::EINVAL;
37+
# when given as `false`, no exceptions are rescued.
38+
#
39+
# Note that these exceptions may be ignored only in `Find` traversal code;
40+
# an exception raised before traversal begins,
41+
# or raised while in the block is not ignored.
42+
# Each of the calls below raises an Errno::ENOENT exception that is not ignored:
43+
#
44+
# ```ruby
45+
# Find.find('nosuch') { }
46+
# Find.find('lib') {|entry| raise Errno::ENOENT }
47+
# ```
48+
#
49+
# With no block given, returns a new Enumerator.
4150
def find(*paths, ignore_error: true) # :yield: path
4251
block_given? or return enum_for(__method__, *paths, ignore_error: ignore_error)
4352

@@ -75,13 +84,26 @@ def find(*paths, ignore_error: true) # :yield: path
7584
nil
7685
end
7786

87+
# :markup: markdown
88+
#
89+
# call-seq:
90+
# Find.prune
91+
#
92+
# This method is meaningful only within a block given with Find.find.
7893
#
79-
# Skips the current file or directory, restarting the loop with the next
80-
# entry. If the current file is a directory, that directory will not be
81-
# recursively entered. Meaningful only within the block associated with
82-
# Find::find.
94+
# Inside such a block,
95+
# "prunes" the traversed file tree by not descending into the current directory:
8396
#
84-
# See the +Find+ module documentation for an example.
97+
# ```ruby
98+
# files = []
99+
# Find.find('.') do |path|
100+
# Find.prune if File.basename(path) == 'test'
101+
# next unless File.file?(path) && File.extname(path) == '.rb'
102+
# files << path
103+
# end
104+
# files.size # => 6690
105+
# files.take(3) # => ["./KNOWNBUGS.rb", "./array.rb", "./ast.rb"]
106+
# ```
85107
#
86108
def prune
87109
throw :prune

0 commit comments

Comments
 (0)