Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > How to detect if variable has been defined yet?

Reply
Thread Tools

How to detect if variable has been defined yet?

 
 
Randy Lawrence
Guest
Posts: n/a
 
      07-02-2004
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
 
Reply With Quote
 
 
 
 
gabriele renzi
Guest
Posts: n/a
 
      07-02-2004
il Fri, 02 Jul 2004 17:40:40 GMT, Randy Lawrence <(E-Mail Removed)>
ha scritto::


defined? may be useful. Anyway, writing:

if variable
variable+=1
else
variable=0
end

should do just fine
 
Reply With Quote
 
 
 
 
Ara.T.Howard
Guest
Posts: n/a
 
      07-02-2004
On Fri, 2 Jul 2004, gabriele renzi wrote:

> il Fri, 02 Jul 2004 17:40:40 GMT, Randy Lawrence <(E-Mail Removed)>
> 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
================================================== =============================
 
Reply With Quote
 
David A. Black
Guest
Posts: n/a
 
      07-02-2004
Hi --

On Sat, 3 Jul 2004, gabriele renzi wrote:

> il Fri, 02 Jul 2004 17:40:40 GMT, Randy Lawrence <(E-Mail Removed)>
> 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

--
David A. Black
http://www.velocityreviews.com/forums/(E-Mail Removed)



 
Reply With Quote
 
David A. Black
Guest
Posts: n/a
 
      07-02-2004
On Sat, 3 Jul 2004, David A. Black wrote:

> 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

--
David A. Black
(E-Mail Removed)



 
Reply With Quote
 
Sean O'Dell
Guest
Posts: n/a
 
      07-02-2004
On Friday 02 July 2004 10:49, Randy Lawrence wrote:
> 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


 
Reply With Quote
 
gabriele renzi
Guest
Posts: n/a
 
      07-02-2004
il Sat, 3 Jul 2004 02:59:52 +0900, "David A. Black"
<(E-Mail Removed)> ha scritto::

oops, sorry for disinforming
 
Reply With Quote
 
Randy Lawrence
Guest
Posts: n/a
 
      07-02-2004
Sean O'Dell wrote:

> On Friday 02 July 2004 10:49, Randy Lawrence wrote:
>
>>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
>
>


Thanks! This was exactly what was needed and it works.
 
Reply With Quote
 
Eric Hodel
Guest
Posts: n/a
 
      07-02-2004
--IYV9cRr2u6rjcP4B
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

David A. Black ((E-Mail Removed)) wrote:

> Hi --
>=20
> On Sat, 3 Jul 2004, gabriele renzi wrote:
>=20
> > il Fri, 02 Jul 2004 17:40:40 GMT, Randy Lawrence <(E-Mail Removed)>
> > 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 - (E-Mail 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)

iD8DBQFA5bBMMypVHHlsnwQRAvAnAKCqeXET7gbj4i3JeVXUUl 7f7sCvPQCfULhX
I0XzuaEeYEs7qghLkU2Uzwo=
=vngq
-----END PGP SIGNATURE-----

--IYV9cRr2u6rjcP4B--


 
Reply With Quote
 
George Ogata
Guest
Posts: n/a
 
      07-02-2004
"David A. Black" <(E-Mail Removed)> writes:

> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
How to detect if web site has been stopped? =?Utf-8?B?SGFycmkgUGVzb25lbg==?= ASP .Net 4 01-20-2006 11:51 AM
The printing has been stopped and this job has been add to the queu? dejola Computer Support 6 12-30-2005 03:26 AM
#if (defined(__STDC__) && !defined(NO_PROTOTYPE)) || defined(__cplusplus) Oodini C Programming 1 09-27-2005 07:58 PM
How to detect that a key is being pressed, not HAS been pressed earlier!?? Rune Python 6 01-29-2004 12:39 PM



Advertisments