From: Hristo Deshev Date: 2005-09-05T13:50:42+09:00 Subject: Re: Help me clean up this method ------=_Part_7656_24751092.1125895837015 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Content-Disposition: inline The mailing list critters ate my indentation! Maybe this one will get=20 through: |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 Hristo Deshev On 9/5/05, Hristo Deshev wrote: >=20 > 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? >=20 >=20 > Hi guys, >=20 > 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= : >=20 > def Dir.size(name) > Dir.chdir(name) > files =3D Dir["**/*"] > files.inject(0) do |total, name| > if File.file?(name) > total + File.size(name) > else > total > end > end > end >=20 > puts Dir.size(".") > puts Dir.size("D:/tmp/ruby") > puts Dir.size("C:/Windows") >=20 > I don't like the "if" statement inside the block that gets injected. Is > there a better, idiomatic way to express the same thing? >=20 > Hristo Deshev >=20 > ------=_Part_7656_24751092.1125895837015--