模块 Find

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

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

require 'find'

total_size = 0

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