From: Daniel Sheppard Date: 2005-09-05T14:34:43+09:00 Subject: Re: 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 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. #####################################################################################