Ruby: Inherit code that works with class variables
The @@variables
aren't class variables. They are class hierarchy variables, i.e. they are shared between the entire class hierarchy, including all subclasses and all instances of all subclasses. (It has been suggested that one should think of @@variables
more like $$variables
, because they actually have more in common with $globals
than with @ivars
. That way lies less confusion. Others have gone further and suggest that they should simply be removed from the language.)
Ruby doesn't have class variables in the sense that, say, Java (where they are called static fields) has them. It doesn't need class variables, because classes are also objects, and so they can have instance variables just like any other object. All you have to do is to remove the extraneous @
s. (And you will have to provide an accessor method for the class instance variable.)
class A def self.init config @config = config end def self.config # This is needed for access from outside @config end def config self.class.config # this calls the above accessor on self's class endend
Let's simplify this a bit, since A.config
is clearly just an attribute_reader:
class A class << self def init config @config = config end attr_reader :config end def config self.class.config endend
And, in fact, A.init
is just a writer with a funny name, so let's rename it to A.config=
and make it a writer, which in turn means that our pair of methods is now just an accessor pair. (Since we changed the API, the test code has to change as well, obviously.)
class A class << self attr_accessor :config end def config self.class.config endendclass B < A; endclass C < A; endB.config = "bar"p B.new.config # => "bar"p C.new.config # => nilC.config = "foo"p B.new.config # => "bar"p C.new.config # => "foo"
However, I can't shake the feeling that there is something more fundamentally iffy about the design, if you need this at all.