Skip to content

Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →

instance method

Find#find

(*::String | ::_ToPath paths, ?ignore_error: ::boolish) -> ::Enumerator[::String, nil]
(*::String | ::_ToPath paths, ?ignore_error: ::boolish) { (::String) -> void } -> nil
find(*paths, ignore_error: true) { |path| ... }

With a block given, performs a depth-first traversal of each given path in paths; calls the block with each found file or directory path:

Example
paths = []
Find.find('bin', 'jit') {|path| paths << path }
paths
# =>
# ["bin",
#  "bin/gem",
#  "jit",
#  "jit/Cargo.toml",
#  "jit/src",
#  "jit/src/lib.rs"]

Raises an exception if a given path cannot be read.

When keyword argument ignore_error is given as true (the default), certain exceptions during traversal are ignored (i.e., silently rescued): Errno::ENOENT, Errno::EACCES, Errno::ENOTDIR, Errno::ELOOP, Errno::ENAMETOOLONG, Errno::EINVAL; when given as false, no exceptions are rescued.

Note that these exceptions may be ignored only in Find traversal code; an exception raised before traversal begins, or raised while in the block is not ignored. Each of the calls below raises an Errno::ENOENT exception that is not ignored:

Example
Find.find('nosuch') { }
Find.find('lib') {|entry| raise Errno::ENOENT }

With no block given, returns a new Enumerator.

See also .find .prune #prune also in 4.0 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0