Created
December 13, 2010 01:44
-
-
Save adzap/738549 to your computer and use it in GitHub Desktop.
Demonstrate issue with class_attribute not cloning hash value in subclasses
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
require 'rubygems' | |
require 'active_support/all' | |
class Base | |
class_attribute :settings | |
class_inheritable_accessor :old_settings | |
self.settings = { :foo => 'bar' } | |
self.old_settings = { :foo => 'bar' } | |
end | |
class Child < Base | |
self.settings.update(:foo => 'child_bar') | |
self.old_settings.update(:foo => 'child_bar') | |
end | |
p "Base: class_attribute - " + Base.settings.inspect | |
p "Base: class_inheritable_accessor - " + Base.old_settings.inspect | |
p "Child: class_attribute - " + Child.settings.inspect | |
p "Child: class_inheritable_accessor - " + Child.old_settings.inspect | |
# On investigation into class_attribute, I read in the AS comments that this is by design. You need to assign a new | |
# object to avoid overwriting the parent value. Like this: | |
Child.settings = Base.settings.merge(:foo => 'child_bar') | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment