I have a number of classes that inherit from one superclass.
The superclass as a module defined. Inside of a module is a self.included(base) method that sets some instance variables.
So something like this:
module MyModule
def self.included(base)
base.instance_variable_set("@my_instance_variable", {})
end
end
class MySuperClass
include MyModule
end
class ClassA < MySuperClass
end
class ClassB < MySuperClass
end
Unless I explicitly include MyModule in ClassA and ClassB then my instance variable will not get set in these two classes.
Is there a way of making sure the modules self.included(base) method is executed in each sub class without the need to explicitly include the module? Since it’s already included in the superclass.
Class instance variable are private to the class. Inherited classes can’t access them directly. There is a couple of ways here.
1. Define accessor
2. More metaprogrammatic
But there’s a hook that gets called every time when a class is inherited from you. You can set variables at this point. Check this out.
NOTE: in this sample each inherited class gets its own class instance var (and base class doesn’t get one). This might be more appropriate for your case, might be not.