Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Newbie Question: Global variable vs. Top-level variable

Reply
Thread Tools

Newbie Question: Global variable vs. Top-level variable

 
 
Sam Kong
Guest
Posts: n/a
 
      05-25-2005
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

 
Reply With Quote
 
 
 
 
Logan Capaldo
Guest
Posts: n/a
 
      05-25-2005
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
>



 
Reply With Quote
 
 
 
 
Joel VanderWerf
Guest
Posts: n/a
 
      05-25-2005
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.


 
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
Initialize global variable before any other global variables jubelbrus C++ 5 07-20-2007 06:38 PM
FWSM/PIX and Dynamic PAT using global IP range vs. global interface vs. global IP Hoffa Cisco 1 10-25-2006 06:50 PM
FWSM/PIX and Dynamic PAT using global IP range vs. global interface vs. global IP Hoffa Cisco 0 10-25-2006 01:04 PM
Is mymodule.myvariable a 'global'? (What is proper definition of 'global variable'?) seberino@spawar.navy.mil Python 1 08-21-2005 09:21 PM
Use Global Variable or Global Struct??? Bryan Parkoff C++ 2 11-27-2004 02:46 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57