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
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
defined? may be useful. Anyway, writing:
if variable
variable+=1
else
variable=0
end
should do just fine
il Fri, 02 Jul 2004 17:40:40 GMT, Randy Lawrence <jm@zzzzzzzzzzzz.com> ha scritto::
unless variable == nil has semantics.
-a
On Fri, 2 Jul 2004, gabriele renzi wrote:
il Fri, 02 Jul 2004 17:40:40 GMT, Randy Lawrence <jm@zzzzzzzzzzzz.com> > ha scritto::
defined? may be useful. Anyway, writing:
if variable
variable+=1
else
variable=0
endshould do just fine
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
===============================================================================
if defined?($total)
$total = $total + 1
else
$total = 0
end
Sean O'Dell
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
Hi --
defined? may be useful. Anyway, writing:
if variable
variable+=1
else
variable=0
endshould 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
On Sat, 3 Jul 2004, gabriele renzi wrote:
il Fri, 02 Jul 2004 17:40:40 GMT, Randy Lawrence <jm@zzzzzzzzzzzz.com> > ha scritto::
--
David A. Black
dblack@wobblini.net
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
endif defined?($total)
$total = $total + 1
else
$total = 0
endSean O'Dell
Thanks! This was exactly what was needed and it works.
Actually to get what Randy wanted it would have to be:
x ||= -1
x += 1
or
x = (x ||= -1) + 1
David
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
--
David A. Black
dblack@wobblini.net
oops, sorry for disinforming 
il Sat, 3 Jul 2004 02:59:52 +0900, "David A. Black" <dblack@wobblini.net> ha scritto::
Use conditional assignment over defined?.
defined? is a sign of code smell. Variables should be explicitly
initialized before use.
David A. Black (dblack@wobblini.net) wrote:
Hi --
On Sat, 3 Jul 2004, gabriele renzi wrote:
> il Fri, 02 Jul 2004 17:40:40 GMT, Randy Lawrence <jm@zzzzzzzzzzzz.com> > > 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 )
--
Eric Hodel - drbrain@segment7.net - http://segment7.net
All messages signed with fingerprint:
FEC2 57F1 D465 EB15 5D6E 7C11 332A 551C 796C 9F04
"David A. Black" <dblack@wobblini.net> 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
"Randy Lawrence" <jm@zzzzzzzzzzzz.com> schrieb im Newsbeitrag
news:JJhFc.31726$eH1.15066611@newssvr28.news.prodigy.com...
Sean O'Dell 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.
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
> On Friday 02 July 2004 10:49, Randy Lawrence wrote: