From: Edward Faulkner Date: 2005-09-05T11:08:22+09:00 Subject: Re: Help me clean up this method --BOKacYhQ+x31HxR3 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Mon, Sep 05, 2005 at 10:52:18AM +0900, James Edward Gray II wrote: > On Sep 4, 2005, at 7:46 PM, Vincent Foley wrote: > > I wrote this little method to return the size of a given directory, =20 > > but I think it's very ugly. Could anyone help me clean it up a bit? >=20 > File.size(dirname) seems to be working on my system. Am I missing =20 > something obvious? File.size(dirname) only tells you how many bytes the directory's own inode is using. It doesn't include the bytes for the directory's files. How about this: def Dir.size(dname) Dir.new(dname).inject(File.size(dname)) {|total,name| begin exname =3D File.expand_path(name,dname) if File.file?(exname)=20 total + File.size(exname) elsif File.directory?(exname) and name !=3D '.' and name !=3D '..' total + Dir.size(exname) else total end rescue total end } end -Ed --BOKacYhQ+x31HxR3 Content-Type: application/pgp-signature; name="signature.asc" Content-Description: Digital signature Content-Disposition: inline -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.1 (GNU/Linux) iD8DBQFDG6iLnhUz11p9MSARAsjyAJ9kVqc0quVzLeGEH2PZBWTzR8+gAQCdGx/0 pSNHg4J5BZzBOWD/pi6P/kQ= =fDxz -----END PGP SIGNATURE----- --BOKacYhQ+x31HxR3--