Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7741523
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T09:03:18+00:00 2026-06-01T09:03:18+00:00

I have a number of classes that inherit from one superclass. The superclass as

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T09:03:18+00:00Added an answer on June 1, 2026 at 9:03 am

    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

    module MyModule
      def self.included(base)
        base.instance_variable_set :@my_instance_variable, {}
        base.extend ClassMethods
      end
    
      module ClassMethods
        def my_instance_variable
          # self is ClassA here, so we need to call superclass
          superclass.instance_variable_get :@my_instance_variable # => {}
        end
      end
    end
    
    class MySuperClass
      include MyModule
    end
    
    class ClassA < MySuperClass; end
    
    ClassA.my_instance_variable # => {}
    

    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.

    module MyModule
      def self.included(base)
        base.extend ClassMethods
      end
    
      module ClassMethods
        # make it a class method. It will be called on target classes later.
        def enhance klass 
          klass.instance_variable_set("@my_instance_variable", {})
        end
      end
    end
    
    class MySuperClass
      include MyModule
    
      # this hook will get called on 'class ClassA < MySuperClass`
      def self.inherited klass
        # set class instance var on child class
        enhance klass
      end
    end
    
    class ClassA < MySuperClass; end
    
    ClassA.instance_variable_get '@my_instance_variable' # => {}
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a base class that a number of other classes inherit from. The
I have a base class called SCO that a number of classes inherit from.
I have a number of LINQ classes that inherit from the same base class.
I have a dll that contains a number of classes that all inherit from
We have a set of about two dozen classes that inherit from a base
I have a class called ItemBase from which a number of classes inherit. In
I've got a number of classes that inherit from Item<T> . Each class has
I have a number of classes that register with a notification class in my
I have a number of classes that are all related conceptually, but some more-so
I have a number of DAO classes that extend SqlMapClientDaoSupport, and call getSqlMapClientTemplate() to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.