Understanding Variables
-
-
@anton_s said:
If those variables would be initialized outside of class...
Your question makes no sense at all.
A class variable, by it's nature, is WITHIN a class, and accessible to all instances of that class. It's like a family value, as opposed to a personal (instance var) value or community (global var) value.
An instance var (@var) is like a personal value. (Thinking of yourself as an instance of class Human,.... how much money YOU have in YOUR pocket, is an instance value.)
How much money ALL of us humans together have in our pockets, would be a class value.
And... DONT create @var instance variables, in the global ObjectSpace (at least in plugins that you distribute.)
Now logically, if your set them both to point at
0
, and test them with==
, you will get a true result, but only for as long as they point at the same object. (Remember in Ruby, the set of Integers is "immediate", meaning their is only 1 of each ordinal in the set. EVER.)OK? There are NOT two variables, each holding a value of
0
.
There ARE two references, each pointing at the same Ruby object,0
which is an instance of classInteger
, and the only one in the set that equals zero, numerically.Ruby has 2 things. Objects and References that point at objects.
If you keep thinking "variables" instead of "references", your will just frustrate yourself.
@var = 8
"The reference@var
shall point at the object8
."
@@var = @var
"The reference@@var
shall point at the object that@var
is pointing at."
@var = 32
"The reference@var
shall be reassigned to point at the object32
."
In the console:
@var
32
@@var
8 -
Okay, now I know!! And yeah at first my question made sense to me, but now it doesn't!
Thanks
Advertisement