From: David Vallner Date: 2006-12-08T17:22:00+09:00 Subject: Re: Default values for method arguments --------------enig76C2E74EC4D20351F9B9017D Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Ken Bloom wrote: > Try this-- it's more concise, and you probably won't feel the need > for more syntactic magic after that. >=20 > def test(args) > args[:a] ||=3D 'defaultA' > args[:b] ||=3D 'defaultB' > args[:c] ||=3D 'defaultC' > puts args.indexes(:a, :b, :c) > end >=20 I find: def test(args) realargs =3D {:a =3D> 'defaultA', :b =3D> 'defaultB', :c =3D> 'defaultC'}.update(args) puts args.indexes(:a, :b, :c) end more readable. Preferrably extracting the default values into constants before the method definition and (deep?)cloning the hash. Also, your code clobbers the hash put into args by putting in values. This might not cause bugs most of the case, but might in case someone passes in an actual hash, so I'd just stay on the safe side. David Vallner --------------enig76C2E74EC4D20351F9B9017D Content-Type: application/pgp-signature; name="signature.asc" Content-Description: OpenPGP digital signature Content-Disposition: attachment; filename="signature.asc" -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.5 (MingW32) iD8DBQFFeSCcy6MhrS8astoRAlSWAJ4j1bfWNoqvQyn9dNfjz0PRsvpITwCdHtak hYW7ABELXOYbcHNR/CgJa2I= =TCMD -----END PGP SIGNATURE----- --------------enig76C2E74EC4D20351F9B9017D--