From: Phrogz Date: 2007-12-12T06:15:11+09:00 Subject: Re: Class variables per subclasses, defined in the superclass On Dec 11, 10:58 am, Gareth Adams wrote: > What I'm trying to do is have a per-subclass variable created by a class method > on the super class (which is called from the subclass). Fun. class Base @thing1 = "Base 1" class << self attr_accessor :thing1 attr_reader :thing2 end def self.thing2=( val ) @thing2 = "I am Thing2, and my value is '#{val}'" end self.thing2 = 'Base' end p Base.thing1, Base.thing2 #=> "Base 1" #=> "I am Thing2, and my value is 'Base'" # Set values for subclasses in the class... class Bar < Base @thing1 = "Barthing1" end # ...or later Bar.thing2 = "Barthing2" p Bar.thing1, Bar.thing2 #=> "Barthing1" #=> "I am Thing2, and my value is 'Barthing2'" # But since class instance variables aren't shared # the base class (and other subclasses) are unchanged p Base.thing1, Base.thing2 #=> "Base 1" #=> "I am Thing2, and my value is 'Base'"