Created
May 22, 2019 14:02
-
-
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
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
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