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 6538487
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T10:44:30+00:00 2026-05-25T10:44:30+00:00

You can call include to mixin a module with a class in ruby, but

  • 0

You can call include to mixin a module with a class in ruby, but it must be done at the beginning of the class definition. Why can’t it be done inside a class function? Is there an alternate syntax?

EX:

module UsefulThings
  def a() puts "a" end
end

class IncludeTester
  include UsefulThings
    def initialize
  end
end

n = IncludeTester.new
n.a()

^^ This works, but if I change IncludeTester to the following, I get the error “undefined method `include'”

class IncludeTester
  def initialize
    include UsefulThings
  end
end
  • 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-05-25T10:44:31+00:00Added an answer on May 25, 2026 at 10:44 am

    It can be done in a class method.

    This works:

    module UsefulThings
      def a
        puts "a" 
      end
    end
    
    class IncludeTester
      def self.mix_in_useful_things
        include UsefulThings
      end 
    end
    
    x = IncludeTester.new
    
    IncludeTester.mix_in_useful_things
    
    x.a # => a
    

    But “initialize” is not a class method, it’s an instance method.

    “new” is a class method. You can think of new as allocating a new object and then calling initialize on it, passing initialize whatever arguments were passed to new.

    You can’t call include directly in initialize because include is a private method of Class (inherited from Module), not of the newly created IncludeTester instance.

    If you want to include a module into a class from an instance method, you have to do something like this:

    class IncludeTester
      def initialize
        self.class.send(:include, UsefulThings)
      end
    end
    

    It’s necessary to use “send” here because include is private method, which means it can only be directly invoked with an implicit receiver (of self).

    When you call initialize normally in a class definition, you’re actually calling it with an implicit receiver of “self”, referring to the class being defined.

    This is what is actually happening when you do this:

    class IncludeTester
      include UsefulThings
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need a function that I can call when somthing is done, for example
We can call destructor explicitly through class pointer, why not constructor? Any idea? #include
I know I can call the GetVersionEx Win32 API function to retrieve the Windows
I noticed that you can call Queue.Synchronize to get a thread-safe queue object, but
How can I bind arguments to a Python function so that I can call
Is there a public/government web service that I can call to find out what
Is there such a thing as an x86 assembler that I can call through
What is the point of having a dynamic class on which you can call
Can I write a custom function that I can call when making mySQL queries
I have this code : include(connect.php); // Call database function $p = 100; $sth

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.