How to detect if variable has been defined yet?

Discussion in 'Ruby' started by Randy Lawrence, Jul 2, 2004.

  1. 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
     
    Randy Lawrence, Jul 2, 2004
    #1
    1. Advertising

  2. il Fri, 02 Jul 2004 17:40:40 GMT, Randy Lawrence <>
    ha scritto::


    defined? may be useful. Anyway, writing:

    if variable
    variable+=1
    else
    variable=0
    end

    should do just fine
     
    gabriele renzi, Jul 2, 2004
    #2
    1. Advertising

  3. Randy Lawrence

    Ara.T.Howard Guest

    On Fri, 2 Jul 2004, gabriele renzi wrote:

    > il Fri, 02 Jul 2004 17:40:40 GMT, Randy Lawrence <>
    > 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
    ===============================================================================
     
    Ara.T.Howard, Jul 2, 2004
    #3
  4. Hi --

    On Sat, 3 Jul 2004, gabriele renzi wrote:

    > il Fri, 02 Jul 2004 17:40:40 GMT, Randy Lawrence <>
    > 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
     
    David A. Black, Jul 2, 2004
    #4
  5. 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
     
    David A. Black, Jul 2, 2004
    #5
  6. Randy Lawrence

    Sean O'Dell Guest

    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
     
    Sean O'Dell, Jul 2, 2004
    #6
  7. il Sat, 3 Jul 2004 02:59:52 +0900, "David A. Black"
    <> ha scritto::

    oops, sorry for disinforming :)
     
    gabriele renzi, Jul 2, 2004
    #7
  8. 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.
     
    Randy Lawrence, Jul 2, 2004
    #8
  9. Randy Lawrence

    Eric Hodel Guest

    --IYV9cRr2u6rjcP4B
    Content-Type: text/plain; charset=us-ascii
    Content-Disposition: inline
    Content-Transfer-Encoding: quoted-printable

    David A. Black () 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 - - 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--
     
    Eric Hodel, Jul 2, 2004
    #9
  10. Randy Lawrence

    George Ogata Guest

    "David A. Black" <> 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
     
    George Ogata, Jul 2, 2004
    #10
  11. "Randy Lawrence" <> schrieb im Newsbeitrag
    news:JJhFc.31726$...
    > 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.


    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
     
    Robert Klemme, Jul 5, 2004
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?SGFycmkgUGVzb25lbg==?=

    How to detect if web site has been stopped?

    =?Utf-8?B?SGFycmkgUGVzb25lbg==?=, Jan 19, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    1,803
    Harri Pesonen
    Jan 20, 2006
  2. Rune
    Replies:
    6
    Views:
    1,312
    Richie Hindle
    Jan 29, 2004
  3. Marcus
    Replies:
    32
    Views:
    951
    Marcus
    Nov 5, 2005
  4. Stef Mientki
    Replies:
    11
    Views:
    751
    Stef Mientki
    Nov 24, 2008
  5. Replies:
    1
    Views:
    103
Loading...

Share This Page