From: Hristo Deshev Date: 2005-09-05T13:45:00+09:00 Subject: Re: Help me clean up this method ------=_Part_7647_10983256.1125895497540 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Content-Disposition: inline On 9/5/05, Vincent Foley wrote: >=20 > Hello guys, >=20 > 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=20 facilities. The size calculation is then a matter of a single inject call: def Dir.size(name) Dir.chdir(name) files =3D Dir["**/*"] files.inject(0) do |total, name|=20 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=20 there a better, idiomatic way to express the same thing? Hristo Deshev ------=_Part_7647_10983256.1125895497540--