How to detect if variable has been defined yet?

R

Randy Lawrence

Hi,

I'd like to detect if a variable has been defined yet. Is this possible
in Ruby or mod_ruby?

For example, I'd like to do the following:

if $total.exists
$total = $total + 1
else
$total = 0
end
 
G

gabriele renzi

il Fri, 02 Jul 2004 17:40:40 GMT, Randy Lawrence <[email protected]>
ha scritto::


defined? may be useful. Anyway, writing:

if variable
variable+=1
else
variable=0
end

should do just fine
 
A

Ara.T.Howard

il Fri, 02 Jul 2004 17:40:40 GMT, Randy Lawrence <[email protected]>
ha scritto::


defined? may be useful. Anyway, writing:

if variable
variable+=1
else
variable=0
end

should do just fine

unless variable == nil has semantics.

-a
--
===============================================================================
| EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov
| PHONE :: 303.497.6469
| A flower falls, even though we love it;
| and a weed grows, even though we do not love it.
| --Dogen
===============================================================================
 
D

David A. Black

Hi --

il Fri, 02 Jul 2004 17:40:40 GMT, Randy Lawrence <[email protected]>
ha scritto::


defined? may be useful. Anyway, writing:

if variable
variable+=1
else
variable=0
end

should do just fine

Have you tried it? :)

$ ruby
if variable; variable += 1; else variable = 0; end
^D
-:1: undefined local variable or method `variable' for
main:Object (NameError)

I think you'd have to use defined?, or conditionally initialize the
variable:

x ||= 0
x += 1

(Note that you can't condense that into (x ||= 0) += 1, though you
could do: x = (x ||= 0) + 1 )


David
 
D

David A. Black

I think you'd have to use defined?, or conditionally initialize the
variable:

x ||= 0
x += 1

Actually to get what Randy wanted it would have to be:

x ||= -1
x += 1

or

x = (x ||= -1) + 1


David
 
S

Sean O'Dell

Hi,

I'd like to detect if a variable has been defined yet. Is this possible
in Ruby or mod_ruby?

For example, I'd like to do the following:

if $total.exists
$total = $total + 1
else
$total = 0
end

if defined?($total)
$total = $total + 1
else
$total = 0
end


Sean O'Dell
 
E

Eric Hodel

--IYV9cRr2u6rjcP4B
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable
Hi --
=20
On Sat, 3 Jul 2004, gabriele renzi wrote:
=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 - (e-mail address removed) - 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--
 
G

George Ogata

David A. Black said:
x ||= 0
x += 1

(Note that you can't condense that into (x ||= 0) += 1, though you
could do: x = (x ||= 0) + 1 )

You can drop the inner assignment too:

x = (x || 0) + 1
 
R

Robert Klemme

Randy Lawrence said:
Thanks! This was exactly what was needed and it works.

Just an additional note: from runtime performance it is much more
efficient to first initialize a variable and then use it. The code looks
cleaner then, too.

$total = 0
....

some loop
$total += 1
end

Regards

robert
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top