Csaba Henk
3/17/2005 7:32:00 AM
On 2005-03-16, Lionel Thiry <lthiryidontwantspam@skynetnospam.be> wrote:
> There's still class instance variable.
>
> class MyClass
> @a = "class instance value"
(snip)
> I prefer that way of doing things over the @@ way.
Yes, you are right, you can in fact go that way. @@ is almost nothing
more than syntax sugar. But syntax sugar is an important component of
the rubyness of ruby...
When I write, "almost", I refer to the following slight difference:
within any subclass of the class defining @@x, it can be accessed
directly, without defining accessor methods and without a reference to
the scope where @@x was introduced. I can't give an example though where
this would have real importance... where you would use the subclass in a
way such that you don't really know in which class has the variable been
defined.
Anyway, if you look into (properly written) ruby code, and you see
@@foo, it sticks out, and you can guess its somewhat specific role in
that class design, and easily identify the way and the reason of its
usage. The same regards to constants, and it's a good thing IMHO.
> With class instance variable, this is easier, I suppose, as you can write:
> class << self
> attr_reader :a
> end
Yes, I know. ATM it's a reason for preferring class instance variables
over class variables. But, what I mean doesn't contradict this. I
just said, it would be good if we had this easy accessors to class
variables as well.
Csaba