On Fri, Jan 21, 2011 at 9:41 PM, Nick Sutterer <[email protected]> wrote:

> 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?

    
http://guides.rubyonrails.org/active_support_core_extensions.html#class-attributes

> 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.

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Core" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-core?hl=en.

Reply via email to