Created
March 28, 2012 15:29
-
-
Save tiegz/2227325 to your computer and use it in GitHub Desktop.
The behavior of superclasses and classes with Rails 3.2 class_attribute
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Key difference: the old way of using {write,read}_inheritable_attribute was | |
| // a *static default* for child classes. The new way of using class_attribute is a | |
| // *dynamic default*. | |
| class A | |
| class_attribute :foo | |
| self.foo = 1 | |
| end | |
| class B < A; end | |
| class C < B; end | |
| A.foo # => 1 | |
| B.foo # => 1 | |
| C.foo # => 1 | |
| A.foo += 1 | |
| A.foo # => 2 | |
| B.foo # => 2 | |
| C.foo # => 2 | |
| B.foo += 1 | |
| A.foo # => 2 | |
| B.foo # => 3 | |
| C.foo # => 3 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment