S
Siemen Baader
Hi,
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!
?> class ORM?> Article.vars
=> ["@@table"]NameError: uninitialized class variable @@table in ORM
from (irb):22:in `table?'
from (irb):31
from :0?> Article.table?
=> :articles
Could anyone enlighten me on why class_variables finds the class
variable in the subclass and why the class methods act like they were
invoked on the parent class? Apparently class_variables finds the
variables in a different way than ruby does when it evaluates the method
code...
Also, has anyone a good idea how I can give each child class variables
which are shared among all instances of the classes but isolated from
parents and other children?
thanks,
siemen baader
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!
?> class ORM?> Article.vars
=> ["@@table"]NameError: uninitialized class variable @@table in ORM
from (irb):22:in `table?'
from (irb):31
from :0?> Article.table?
=> :articles
Could anyone enlighten me on why class_variables finds the class
variable in the subclass and why the class methods act like they were
invoked on the parent class? Apparently class_variables finds the
variables in a different way than ruby does when it evaluates the method
code...
Also, has anyone a good idea how I can give each child class variables
which are shared among all instances of the classes but isolated from
parents and other children?
thanks,
siemen baader