Gary Wright
3/27/2007 11:38:00 PM
On Mar 27, 2007, at 6:50 PM, Siemen Baader wrote:
> I am writing an object relational mapper for an educational project
> where I need to add new class variables to classes in an
> inheritance hierarchy. The variables hold class-specific
> information such as which tables are mapped and validation logic
> so they cannot be shared among the hierarchy like inherited class
> variables from the superclass would. Because of that I could not
> declare them in the parent and just initialize them in the
> subclasses. Instead, I decided to add and set them at runtime after
> subclassing.
>
> However, i think ruby behaves a bit odd here. In class methods
> inherited from the superclass I can see the class variables through
> the method class_variables, but to use them in the child class I
> have to re-open the children and re-define the class methods which
> use the variables!
Don't use Ruby 'class variables' use Ruby 'class instance variables'.
Programmers coming to Ruby and expecting per class state often gravitate
towards the mis-named 'class variables' when what they *really* want are
'class instance variables'.
I fault the misleading name and explanations in Pickaxe and The Ruby Way
for this never ending source of confusion. For example on p. 33 of
the Pickaxe:
"Sometimes classes themselves need to have their own states. This is
where class variables come in."
And on page 3 and 4 of The Ruby Way is a several paragraph
description of
per class state that consistently uses the term 'class variable' when,
with respect to Ruby, 'class instance variable' is more applicable.
Gary Wright