T
Thibaut Barrère
Hi guys,
I'm computing an approximate folder size by summing the sizes of its
files, recursively, with the following code:
def item_size(name)
if File.directory?(name)
(Dir.entries(name) - ['.','..']).inject(0) { |sum,file| sum +
item_size(File.join(name,file)) }
else
File.size(name)
end
end
Another version would be:
def item_size_2(name)
Dir[name+'/**/*'].inject(0) { |sum,file| sum + (File.directory?
(file) ? 0 : File.size(file)) }
end
can anyone think of a shorter or more readable way of writing it ? Do
you know a library that would do this ?
cheers,
Thibaut Barrère / LoGeek
I'm computing an approximate folder size by summing the sizes of its
files, recursively, with the following code:
def item_size(name)
if File.directory?(name)
(Dir.entries(name) - ['.','..']).inject(0) { |sum,file| sum +
item_size(File.join(name,file)) }
else
File.size(name)
end
end
Another version would be:
def item_size_2(name)
Dir[name+'/**/*'].inject(0) { |sum,file| sum + (File.directory?
(file) ? 0 : File.size(file)) }
end
can anyone think of a shorter or more readable way of writing it ? Do
you know a library that would do this ?
cheers,
Thibaut Barrère / LoGeek