V
Vincent Foley
Hello guys,
I wrote this little method to return the size of a given directory, but
I think it's very ugly. Could anyone help me clean it up a bit?
def Dir.size(dirname)
Dir.chdir(dirname)
entries = Dir.entries(".").reject { |x| %w(. ..).include? x }
entries.collect! { |filename| File.expand_path(filename) }
size = 0
entries.each do |filename|
begin
if File.file?(filename)
size += File.size(filename) rescue 0
else
size += Dir.size(filename)
end
rescue
next
end
end
size
end
Thanks,
Vincent.
I wrote this little method to return the size of a given directory, but
I think it's very ugly. Could anyone help me clean it up a bit?
def Dir.size(dirname)
Dir.chdir(dirname)
entries = Dir.entries(".").reject { |x| %w(. ..).include? x }
entries.collect! { |filename| File.expand_path(filename) }
size = 0
entries.each do |filename|
begin
if File.file?(filename)
size += File.size(filename) rescue 0
else
size += Dir.size(filename)
end
rescue
next
end
end
size
end
Thanks,
Vincent.