module Find

TheFind module supports the top-down traversal of a set of file paths.

For example, to total the size of all files under your home directory, ignoring anything in a “dot” directory (e.g. $HOME/.ssh):

require'find'total_size =0Find.find(ENV["HOME"])do|path|ifFileTest.directory?(path)ifFile.basename(path).start_with?('.')Find.prune# Don't look any further into this directory.elsenextendelsetotal_size+=FileTest.size(path)endend

Constants

VERSION

The version string

Public Class Methods

Source
# File lib/find.rb, line 41deffind(*paths,ignore_error:true)# :yield: pathblock_given?orreturnenum_for(__method__,*paths,ignore_error:ignore_error)fs_encoding =Encoding.find("filesystem")paths.collect!{|d|raiseErrno::ENOENT,dunlessFile.exist?(d);d.dup}.eachdo|path|path =path.to_pathifpath.respond_to?:to_pathenc =path.encoding==Encoding::US_ASCII?fs_encoding:path.encodingps = [path]whilefile =ps.shiftcatch(:prune)doyieldfile.dupbegins =File.lstat(file)rescueErrno::ENOENT,Errno::EACCES,Errno::ENOTDIR,Errno::ELOOP,Errno::ENAMETOOLONG,Errno::EINVALraiseunlessignore_errornextendifs.directory?thenbeginfs =Dir.children(file,encoding:enc)rescueErrno::ENOENT,Errno::EACCES,Errno::ENOTDIR,Errno::ELOOP,Errno::ENAMETOOLONG,Errno::EINVALraiseunlessignore_errornextendfs.sort!fs.reverse_each {|f|f =File.join(file,f)ps.unshiftf          }endendendendnilend

Calls the associated block with the name of every file and directory listed as arguments, then recursively on their subdirectories, and so on.

Returns an enumerator if no block is given.

See theFind module documentation for an example.

Source
# File lib/find.rb, line 86defprunethrow:pruneend

Skips the current file or directory, restarting the loop with the next entry. If the current file is a directory, that directory will not be recursively entered. Meaningful only within the block associated withFind::find.

See theFind module documentation for an example.

Private Instance Methods

Source
# File lib/find.rb, line 41deffind(*paths,ignore_error:true)# :yield: pathblock_given?orreturnenum_for(__method__,*paths,ignore_error:ignore_error)fs_encoding =Encoding.find("filesystem")paths.collect!{|d|raiseErrno::ENOENT,dunlessFile.exist?(d);d.dup}.eachdo|path|path =path.to_pathifpath.respond_to?:to_pathenc =path.encoding==Encoding::US_ASCII?fs_encoding:path.encodingps = [path]whilefile =ps.shiftcatch(:prune)doyieldfile.dupbegins =File.lstat(file)rescueErrno::ENOENT,Errno::EACCES,Errno::ENOTDIR,Errno::ELOOP,Errno::ENAMETOOLONG,Errno::EINVALraiseunlessignore_errornextendifs.directory?thenbeginfs =Dir.children(file,encoding:enc)rescueErrno::ENOENT,Errno::EACCES,Errno::ENOTDIR,Errno::ELOOP,Errno::ENAMETOOLONG,Errno::EINVALraiseunlessignore_errornextendfs.sort!fs.reverse_each {|f|f =File.join(file,f)ps.unshiftf          }endendendendnilend

Calls the associated block with the name of every file and directory listed as arguments, then recursively on their subdirectories, and so on.

Returns an enumerator if no block is given.

See theFind module documentation for an example.

Source
# File lib/find.rb, line 86defprunethrow:pruneend

Skips the current file or directory, restarting the loop with the next entry. If the current file is a directory, that directory will not be recursively entered. Meaningful only within the block associated withFind::find.

See theFind module documentation for an example.