Skip to content

Instantly share code, notes, and snippets.

@tiegz
Created March 28, 2012 15:29
Show Gist options
  • Select an option

  • Save tiegz/2227325 to your computer and use it in GitHub Desktop.

Select an option

Save tiegz/2227325 to your computer and use it in GitHub Desktop.
The behavior of superclasses and classes with Rails 3.2 class_attribute
// 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