Help me clean up this method

This is cleaner to my eye, but YMMV.

class Dir
def size(name)
   Dir.chdir(name)
   Dir["**/*"].inject(0) do |total,name|
     total + (File.file?(name) ? File.size(name) : 0)
   end
end
end

Also, be aware that you're changing the working dir and not changing it
back.

Are there situations where Dir[name + "/**/*"] wouldn't work?

···

-----Original Message-----
From: Hristo Deshev [mailto:hristo.deshev@gmail.com]
Sent: Monday, 5 September 2005 2:45 PM
To: ruby-talk ML
Subject: Re: Help me clean up this method

On 9/5/05, Vincent Foley <vfoley@gmail.com> wrote:

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?

Hi guys,

I managed to get rid of the file names discovery by using Dir's globbing
facilities. The size calculation is then a matter of a single inject
call:

def Dir.size(name)
Dir.chdir(name)
files = Dir["**/*"]
files.inject(0) do |total, name|
if File.file?(name)
total + File.size(name)
else
total
end
end
end

puts Dir.size(".")
puts Dir.size("D:/tmp/ruby")
puts Dir.size("C:/Windows")

I don't like the "if" statement inside the block that gets injected. Is
there a better, idiomatic way to express the same thing?

Hristo Deshev

#####################################################################################
This email has been scanned by MailMarshal, an email content filter.
#####################################################################################

Hi Daniel,

This is cleaner to my eye, but YMMV.

>class Dir
> def size(name)
> Dir.chdir(name)
> Dir["**/*"].inject(0) do |total,name|
> total + (File.file?(name) ? File.size(name) : 0)
> end
> end
>end

The ?: operator is indeed shorter. I am not sure if I like it better, but I
will stick with it. Maybe extracting the file or dir size calculation to a
separate method would be best.

Also, be aware that you're changing the working dir and not changing it

back.

Are there situations where Dir[name + "/**/*"] wouldn't work?

I can swear I tried that first and it did not return any files. Maybe I was
missing a slash somewhere. It now works like a charm. Thanks for pointing it
out.

Hristo Deshev

···

On 9/5/05, Daniel Sheppard <daniels@pronto.com.au> wrote:

Not to be picky, but most of the above examples need another rescue
statement or two. In particular, Dir.chdir(name) can throw an exception, at
least on Windows XP:

================ Errno::EACCES =====================
C:\Documents and Settings\All Users\Documents\test.rb:10:in `chdir'
Dir.chdir('c:/System Volume Information')
C:\Documents and Settings\All Users\Documents\test.rb:10
Dir.chdir('c:/System Volume Information')
c:\ruby\lib\ruby\site_ruby\1.8/rubygems/custom_require.rb:21:in `require__'
require__ path
c:\ruby\lib\ruby\site_ruby\1.8/rubygems/custom_require.rb:21:in `require'
require__ path

···

=============================================
Exception: Permission denied - c:/System Volume Information

Wayne Vucenic
No Bugs Software
Ruby and C++ Contract Programming in Silicon Valley