首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

Find

Find模块支持自顶向下遍历一组文件路径。

例如,要总计主目录下所有文件的大小,忽略“dot”目录中的任何内容(例如$ HOME / .ssh):

代码语言:javascript
复制
require 'find'

total_size = 0

Find.find(ENV["HOME"]) do |path|
  if FileTest.directory?(path)
    if File.basename(path)[0] == ?.
      Find.prune       # Don't look any further into this directory.
    else
      next
    end
  else
    total_size += FileTest.size(path)
  end
end

Public Class Methods

find(*paths, ignore_error: true) { |path| ... } Show source

调用相关的块,将每个文件和目录的名称列为参数,然后递归地在其子目录中,依此类推。

如果没有给出块,则返回一个枚举器。

有关Find示例,请参阅模块文档。

代码语言:javascript
复制
# File lib/find.rb, line 37
def find(*paths, ignore_error: true) # :yield: path
  block_given? or return enum_for(__method__, *paths, ignore_error: ignore_error)

  fs_encoding = Encoding.find("filesystem")

  paths.collect!{|d| raise Errno::ENOENT, d unless File.exist?(d); d.dup}.each do |path|
    path = path.to_path if path.respond_to? :to_path
    enc = path.encoding == Encoding::US_ASCII ? fs_encoding : path.encoding
    ps = [path]
    while file = ps.shift
      catch(:prune) do
        yield file.dup.taint
        begin
          s = File.lstat(file)
        rescue Errno::ENOENT, Errno::EACCES, Errno::ENOTDIR, Errno::ELOOP, Errno::ENAMETOOLONG
          raise unless ignore_error
          next
        end
        if s.directory? then
          begin
            fs = Dir.entries(file, encoding: enc)
          rescue Errno::ENOENT, Errno::EACCES, Errno::ENOTDIR, Errno::ELOOP, Errno::ENAMETOOLONG
            raise unless ignore_error
            next
          end
          fs.sort!
          fs.reverse_each {|f|
            next if f == "." or f == ".."
            f = File.join(file, f)
            ps.unshift f.untaint
          }
        end
      end
    end
  end
  nil
end

prune() Show source

跳过当前文件或目录,用下一个条目重新启动循环。如果当前文件是目录,则不会递归输入该目录。只在与:: find相关的块内有意义。

有关Find示例,请参阅模块文档。

代码语言:javascript
复制
# File lib/find.rb, line 83
def prune
  throw :prune
end

私有实例方法

find(*paths, ignore_error: true) { |path| ... } Show source

调用相关的块,将每个文件和目录的名称列为参数,然后递归地在其子目录中,依此类推。

如果没有给出块,则返回一个枚举器。

有关Find示例,请参阅模块文档。

代码语言:javascript
复制
# File lib/find.rb, line 37
def find(*paths, ignore_error: true) # :yield: path
  block_given? or return enum_for(__method__, *paths, ignore_error: ignore_error)

  fs_encoding = Encoding.find("filesystem")

  paths.collect!{|d| raise Errno::ENOENT, d unless File.exist?(d); d.dup}.each do |path|
    path = path.to_path if path.respond_to? :to_path
    enc = path.encoding == Encoding::US_ASCII ? fs_encoding : path.encoding
    ps = [path]
    while file = ps.shift
      catch(:prune) do
        yield file.dup.taint
        begin
          s = File.lstat(file)
        rescue Errno::ENOENT, Errno::EACCES, Errno::ENOTDIR, Errno::ELOOP, Errno::ENAMETOOLONG
          raise unless ignore_error
          next
        end
        if s.directory? then
          begin
            fs = Dir.entries(file, encoding: enc)
          rescue Errno::ENOENT, Errno::EACCES, Errno::ENOTDIR, Errno::ELOOP, Errno::ENAMETOOLONG
            raise unless ignore_error
            next
          end
          fs.sort!
          fs.reverse_each {|f|
            next if f == "." or f == ".."
            f = File.join(file, f)
            ps.unshift f.untaint
          }
        end
      end
    end
  end
  nil
end

prune() Show source

跳过当前文件或目录,用下一个条目重新启动循环。如果当前文件是目录,则不会递归输入该目录。只在与:: find相关的块内有意义。

有关Find示例,请参阅模块文档。

代码语言:javascript
复制
# File lib/find.rb, line 83
def prune
  throw :prune
end

扫码关注腾讯云开发者

领取腾讯云代金券