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

  • Home
  • SEARCH
  • 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 8600349
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T01:35:50+00:00 2026-06-12T01:35:50+00:00

Is there any clean way to initialize instance variables in a Module intended to

  • 0

Is there any clean way to initialize instance variables in a Module intended to be used as Mixin? For example, I have the following:

module Example

  def on(...)   
    @handlers ||= {} 
    # do something with @handlers
  end

  def all(...)
    @all_handlers ||= []
    # do something with @all_handlers
  end

  def unhandled(...)
    @unhandled ||= []
    # do something with unhandled
  end

  def do_something(..)
    @handlers     ||= {}
    @unhandled    ||= []
    @all_handlers ||= []

    # potentially do something with any of the 3 above
  end

end

Notice that I have to check again and again if each @member has been properly initialized in each function — this is mildly irritating. I would much rather write:

module Example

  def initialize
    @handlers     = {}
    @unhandled    = []
    @all_handlers = []
  end

  # or
  @handlers  = {}
  @unhandled = []
  # ...
end

And not have to repeatedly make sure things are initialized correctly. However, from what I can tell this is not possible. Is there any way around this, besides adding a initialize_me method to Example and calling initialize_me from the extended Class? I did see this example, but there’s no way I’m monkey-patching things into Class just to accomplish this.

  • 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-12T01:35:51+00:00Added an answer on June 12, 2026 at 1:35 am
    module Example
      def self.included(base)
        base.instance_variable_set :@example_ivar, :foo
      end
    end
    

    Edit: Note that this is setting a class instance variable. Instance variables on the instance can’t be created when the module is mixed into the class, since those instances haven’t been created yet. You can, though, create an initialize method in the mixin, e.g.:

    module Example
      def self.included(base)
        base.class_exec do
          def initialize
            @example_ivar = :foo
          end
        end
      end
    end
    

    There may be a way to do this while calling the including class’s initialize method (anybody?). Not sure. But here’s an alternative:

    class Foo
      include Example
    
      def initialize
        @foo = :bar
        after_initialize
      end
    end
    
    module Example
      def after_initialize
        @example_ivar = :foo
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

is there any clean way of doing this that works in all browsers?
Is there a clean way to somehow use underscore.js _.extend function (or any other)
I have a plugin which is applied on few element. Is there any way
is there any clean way to get hold of the rJava classloader? Unfortunately I
Is there any clean way to allow a user to select multiple non continuos
In ASP.NET, is there any way to get the real raw URL? For example,
Is there any way to clear parser buffers before calling YYACCEPT in yacc .
Are there any automated tools that will parse through XAML code and clean out
Is there any way in Notepad++ (or even with another tool) to change the
Is there any way I can set a formatter on models that will convert

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.