Newbie Question: Global variable vs. Top-level variable

Discussion in 'Ruby' started by Sam Kong, May 25, 2005.

  1. Sam Kong

    Sam Kong Guest

    Hi, group!

    This morning, a question came into my mind (I'm still a newbie).
    What's the meaning of a top-level variable?

    t = "top"
    $g = "global"

    puts t
    puts $g

    def f
    puts t #error
    puts $g
    end

    f

    I know that a top-level variable cannot be used in a method.

    My questions are:

    1. Do you use top-level variables instead of global variables if you
    don't need to use it in a method? Is it a good practice to use it like
    that?

    2. What is a top-level variable? Is it an object variable or a local
    variable or what?

    3. What is the exact scope of a top-level variable?


    Thanks.
    Sam
     
    Sam Kong, May 25, 2005
    #1
    1. Advertising

  2. On 5/25/05, Sam Kong <> wrote:
    > Hi, group!
    >=20
    > This morning, a question came into my mind (I'm still a newbie).
    > What's the meaning of a top-level variable?
    >=20
    > t =3D "top"
    > $g =3D "global"
    >=20
    > puts t
    > puts $g
    >=20
    > def f
    > puts t #error
    > puts $g
    > end
    >=20
    > f
    >=20
    > I know that a top-level variable cannot be used in a method.
    >=20
    > My questions are:
    >=20
    > 1. Do you use top-level variables instead of global variables if you
    > don't need to use it in a method? Is it a good practice to use it like
    > that?
    >=20


    If you're writing a short script with no methods (or very few) its a
    not a big deal.

    > 2. What is a top-level variable? Is it an object variable or a local
    > variable or what?
    >

    It's a lexically scoped local. You can imagine the top level as sort
    of being inside it's own implicit method that automatically gets
    called. (not really true, but works as far as scope is concernced.
    > 3. What is the exact scope of a top-level variable?
    >=20
    >=20

    Well liek I said its a local, and lexically scoped. So....

    x =3D 1

    def a
    x #error
    end

    # blocks are closures so....

    c=3D lambda do |a|
    x + a # ok
    end

    class Q
    x #also an error
    end



    > Thanks.
    > Sam
    >=20
    >=20
    >
     
    Logan Capaldo, May 25, 2005
    #2
    1. Advertising

  3. Logan Capaldo wrote:
    > On 5/25/05, Sam Kong <> wrote:

    ...
    >>2. What is a top-level variable? Is it an object variable or a local
    >>variable or what?
    >>

    >
    > It's a lexically scoped local. You can imagine the top level as sort
    > of being inside it's own implicit method that automatically gets
    > called. (not really true, but works as far as scope is concernced.
    >
    >>3. What is the exact scope of a top-level variable?
    >>
    >>

    >
    > Well liek I said its a local, and lexically scoped. So....


    ...and the scope is the _file_ in which that variable appears.
     
    Joel VanderWerf, May 25, 2005
    #3
    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. Frederic HOUDE
    Replies:
    4
    Views:
    599
    Frederic HOUDE
    Aug 31, 2004
  2. pabbu
    Replies:
    8
    Views:
    733
    Marc Boyer
    Nov 7, 2005
  3. Cuthbert
    Replies:
    8
    Views:
    437
    Ancient_Hacker
    Sep 13, 2006
  4. Bret
    Replies:
    4
    Views:
    289
  5. Kenneth McDonald

    Newbie question on RDoc--top-level functions

    Kenneth McDonald, Nov 4, 2008, in forum: Ruby
    Replies:
    2
    Views:
    87
    Ryan Davis
    Nov 4, 2008
Loading...

Share This Page