Thibaut BarrīĶe wrote:
> 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īĶe / LoGeek
> --
> http://blog.logeek.fr - learning content for developers
> http://evolvingworker.com - tools for a better day
> 

Maybe use the Find module?

#!/usr/bin/env ruby
require 'find'

size = 0
Find.find(".") do|f|
   size += File.size(f) if File.file?(f)
end

puts size

-- 
Michael Morin
Guide to Ruby
http://ruby.about.com/
Become an About.com Guide:  beaguide.about.com
About.com is part of the New York Times Company