Ruby Newbie: What if @ symbol is not used?

T

Todd Gardner

Hello Everyone,

Pardon this newbie question.

What if the @ symbol is not used before creating new variable names
inside of methods?

Thanks,

Todd
 
A

Ara.T.Howard

Hello Everyone,

Pardon this newbie question.

What if the @ symbol is not used before creating new variable names
inside of methods?

Thanks,

Todd

then the variables are local to the method. @vars are always instance vars of
the object - they persist after the method invocation and are availble to any
method of the object. this analogy may help (although it is not really
correct):

- @vars are 'global' within an instance of a class - an object. all methods
of the object may see this variable and have unrestricted read/write
access to this @var. other objects of the same class have their own
unique copies of @vars however, and cannot see the @vars of other objects.

- @@vars are 'global' to all instances of a class. all objects of a given
class may see this variable. there is only one copy per class and all
objects of that class may read/write @@vars.

- 'normal' vars are just that, no special scoping rules, etc. they are
local to the method in which they were defined and leave scope (destroyed)
after the method has ended.

cheers.

-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
===============================================================================
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,148
Messages
2,570,834
Members
47,380
Latest member
AlinaBlevi

Latest Threads

Top