Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save zdennis/3178ec16f9d9f9c54f9897a8a8130dc4 to your computer and use it in GitHub Desktop.
Save zdennis/3178ec16f9d9f9c54f9897a8a8130dc4 to your computer and use it in GitHub Desktop.
Differences with Rails 2.3 class_inheritable_accessor and Rails 3.2+ class_attribute
class Task
class_inheritable_accessor :foo
self.foo = []
class_attribute :bar
self.bar = []
end
class Subtask < Task
end
Task.foo.object_id == Subtask.foo.object_id
# => false
Task.bar.object_id == Subtask.bar.object_id
# true
Task.foo << 'hi'
Subtask.foo << 'bye'
Task.foo # => ['hi']
Subtask.foo # => ['bye']
Task.bar << 'hi'
Subtask.bar << 'bye'
Task.bar # => ['hi', 'bye']
Subtask.bar # => ['hi', 'bye']
=begin
`foo` gets dup’d when subclasses so each class gets its own copy of an
empty array whereas `bar`inherits the same reference by the default.
=end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment