Ruby: Inherit code that works with class variables
问题 The situation: I have multiple classes that should each hold a variable with a configuration hash; a different hash for each class but the same for all instances of a class. At first, i tried like this class A def self.init config @@config = config end def config @@config end end class B < A; end class C < A; end But soon noticed that it wouldn't work that way because @@config is held in the context of A, not B or C, thus: B.init "bar" p B.new.config # => "bar" p C.new.config # => "bar" -