|
3 | 3 | # find.rb: the Find module for processing all files under a given directory. |
4 | 4 | # |
5 | 5 |
|
| 6 | +# :markup: markdown |
6 | 7 | # |
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. |
28 | 9 | moduleFind |
29 | 10 |
|
30 | 11 | # The version string |
31 | 12 | VERSION="0.2.0" |
32 | 13 |
|
| 14 | +# :markup: markdown |
33 | 15 | # |
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: |
36 | 18 | # |
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 | +# ``` |
38 | 31 | # |
39 | | -# See the +Find+ module documentation for an example. |
| 32 | +# Raises an exception if a given path cannot be read. |
40 | 33 | # |
| 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. |
41 | 50 | deffind(*paths,ignore_error: true)# :yield: path |
42 | 51 | block_given?orreturnenum_for(__method__, *paths,ignore_error: ignore_error) |
43 | 52 |
|
@@ -75,13 +84,26 @@ def find(*paths, ignore_error: true) # :yield: path |
75 | 84 | nil |
76 | 85 | end |
77 | 86 |
|
| 87 | +# :markup: markdown |
| 88 | +# |
| 89 | +# call-seq: |
| 90 | +# Find.prune |
| 91 | +# |
| 92 | +# This method is meaningful only within a block given with Find.find. |
78 | 93 | # |
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: |
83 | 96 | # |
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 | +# ``` |
85 | 107 | # |
86 | 108 | defprune |
87 | 109 | throw:prune |
|
0 commit comments