module Find

Find 模块支持文件路径集合的自顶向下遍历。

例如,计算你的主目录下的所有文件的大小,忽略“点”(dot)目录(例如 $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