From: Eric Hodel Date: 2004-07-03T03:58:21+09:00 Subject: Re: How to detect if variable has been defined yet? --IYV9cRr2u6rjcP4B Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable David A. Black (dblack@wobblini.net) wrote: > Hi -- >=20 > On Sat, 3 Jul 2004, gabriele renzi wrote: >=20 > > il Fri, 02 Jul 2004 17:40:40 GMT, Randy Lawrence > > ha scritto:: > >=20 > >=20 > > defined? may be useful. Anyway, writing: > >=20 > > if variable > > variable+=3D1 > > else > > variable=3D0 > > end > >=20 > > should do just fine > >=20 >=20 > Have you tried it? :-) >=20 > $ ruby > if variable; variable +=3D 1; else variable =3D 0; end > ^D > -:1: undefined local variable or method `variable' for > main:Object (NameError) >=20 > I think you'd have to use defined?, or conditionally initialize the > variable: >=20 > x ||=3D 0 > x +=3D 1 >=20 > (Note that you can't condense that into (x ||=3D 0) +=3D 1, though you > could do: x =3D (x ||=3D 0) + 1 ) Use conditional assignment over defined?. defined? is a sign of code smell. Variables should be explicitly initialized before use. --=20 Eric Hodel - drbrain@segment7.net - http://segment7.net All messages signed with fingerprint: FEC2 57F1 D465 EB15 5D6E 7C11 332A 551C 796C 9F04 --IYV9cRr2u6rjcP4B Content-Type: application/pgp-signature Content-Disposition: inline -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.2.2 (FreeBSD) iD8DBQFA5bBMMypVHHlsnwQRAvAnAKCqeXET7gbj4i3JeVXUUl7f7sCvPQCfULhX I0XzuaEeYEs7qghLkU2Uzwo= =vngq -----END PGP SIGNATURE----- --IYV9cRr2u6rjcP4B--