According to the docs, class_attribute does implement inheritance for
class instance variables. However, it doesn't work as it is intended
to, at least if you use "mutual structures", like Hash.
Base.foo = {}
Subclass.foo[:bar] = "bar"
Base.foo # => {:bar => "bar"}
I'm sorry, but this is WRONG and dangerous. The docs tell me to "use
setters" here - not sure how this is supposed to work.
I like Nick's implementation, not everyone know or remembers that
modyfing object like Hash or Array will also change the superclass
(which is clearly visible in the commit that Nick's linked - that was
exactly the reason of the bug that I was fixing).
I think that it would be nice to have inheritable class attributes
that allows to forget about that issue.
According to the docs, class_attribute does implement inheritance for
class instance variables. However, it doesn't work as it is intended
to, at least if you use "mutual structures", like Hash.
Base.foo = {}
Subclass.foo[:bar] = "bar"
Base.foo # => {:bar => "bar"}
I'm sorry, but this is WRONG and dangerous.
This particular macro does not implement cloning. Not doing so is not
wrong or right a priori, it is just the way it works.
The docs tell me to "use
setters" here - not sure how this is supposed to work.
Have you read the explanation in the AS core extensions guide?
SO, if I need to initialize the ivar, I don't need inheritance. Why is
that called "inheritance" at all?
Well, because they are inherited in the sense that if A < B and B has
a class attribute "x", then A also has "x" and it has the same value
(the same object reference) than B by default.
They are different from ordinary Ruby class variables in that you can
*override* them at any level in the hierarchy. They also provide
access at the instance level.
Please have a look also to the section about class inheritable attributes.
> It's simple, clean and does exactly what you expect.
Well, as long as "what you expect" doesn't involve keeping anything more complicated than a flat Hash/Array/etc in the class variable:
Ouuuuuuuuuuuuuuuuuuuuuch - that's absolutely right. So worst case is
that in my solution you might still operate on a superclass object.
Let me think about that!
Ouuuuuuuuuuuuuuuuuuuuuch - that's absolutely right. So worst case is
that in my solution you might still operate on a superclass object.
Let me think about that!
We have several different options for this depending on the semantics
you're after for your 'class variable things'.
Does something closer to what you have in mind, but you still will
have child classes able to alter the parent's Hashes.
The problem is that you're expecting class_attribute to magically
clone itself because you're storing a Hash there. However if instead
of a Hash you were storing something else, say a database connection,
then your solution will be confusing and cause strange errors.
According to the docs, class_attribute does implement inheritance for
class instance variables. However, it doesn't work as it is intended
to, at least if you use "mutual structures", like Hash.
Base.foo = {}
Subclass.foo[:bar] = "bar"
Base.foo # => {:bar => "bar"}
I'm sorry, but this is WRONG and dangerous. The docs tell me to "use
setters" here - not sure how this is supposed to work.