benjohn
3/17/2006 12:05:00 AM
On 15 Mar 2006, at 00:54, Jeff Cohen wrote:
> rtilley wrote:
>> This may be wrong, but I like to think of @@ as a class variable
>> and @
>> as a method variable. @@ is accessible by any method in the class
>> and @
>> is accessible by the method only.
>
> You're close. @ are instance variables. Any instance method in your
> class can
> access them.
You can also access @ variables from a class method - ie
ClassName.function; @a_variable; end. They are local to each class
within a hierarchy, rather than @@ which would be shared among
classes in the hierarchy.
So:
class X
def X.var;
@var
end
def X.var=(v)
@var=v
end
end
class Y<X;end
X.var=10
Y.var=20
X.var => 10
Y.var => 20
Cheers,
Benjohn