Skip to content

Instantly share code, notes, and snippets.

@eterps
Created July 26, 2010 15:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eterps/490671 to your computer and use it in GitHub Desktop.
Save eterps/490671 to your computer and use it in GitHub Desktop.
class DirTree < String
include Enumerable
def each
Dir.entries(self).reject{|n| %w[. ..].include? n}.each do |filename|
yield path = "#{self}/#{filename}"
DirTree.new(path).each{|n| yield n} if File.directory? path
end
end
end
class DiskUsage
def self.[](path)
DirTree.new(path).map{|n| File.lstat(n).size}.inject(0){|sum, size| sum + size}
end
end
p DiskUsage['.']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment