Skip to content

Instantly share code, notes, and snippets.

@adzap
Created December 13, 2010 01:44
Show Gist options
  • Save adzap/738549 to your computer and use it in GitHub Desktop.
Save adzap/738549 to your computer and use it in GitHub Desktop.
Demonstrate issue with class_attribute not cloning hash value in subclasses
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