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

The Archive Base Latest Questions

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

test_module.rb module MyModule def module_func_a puts module_func_a invoked private_b end module_function :module_func_a private def

  • 0

test_module.rb

  module MyModule
    def module_func_a
      puts "module_func_a invoked"
      private_b
    end

    module_function :module_func_a

    private
    def private_b
      puts "private_b invoked"
    end
  end

  class MyClass
    include MyModule
    def test_module
      module_func_a
    end
  end

Invoking module function from class

  c = MyClass.new
  c.test_module

Output 1:

$ ruby test_module.rb 
 module_func_a invoked
 private_b invoked

Invoking module function on module in class method style

ma = MyModule.module_func_a

Output 2:

 module_func_a invoked
 test_module.rb:5:in `module_func_a': undefined local variable or method `private_b' for MyModule:Module (NameError)
 from test_module.rb:31

As can be seen from the Output 1 and Output 2 when including the module in a class, no issue occurs when a module’s private method gets invoked from a module function
while in case when directly invoking the module function on the module in class method style the module’s private method, invoked from module function, is not found.

Can anybody make me understand the reason behind above behavior and whether invoking module function (which in turn invokes module’s private method) on module in class method style is possible or not? If possible, then what rectifications are required in my code to do the same?

  • 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-01T15:03:45+00:00Added an answer on June 1, 2026 at 3:03 pm

    It works when you include the module in a class, because then all of the module’s methods get included in that class (the self in module_func_a points to MyClass, which has also the private_b method).

    In the other situation self points to MyModule, which does not have private_b method. If you wanted it to work both ways, you’d have to either declare private_b as a module method as well, or simply add the line extend self to MyModule, so that all it’s methods would become module methods.

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

Sidebar

Related Questions

module Test def self.model_method puts this is a module method end end class A
I'd like to include TestModule in MyModule : # in test_module.rb module TestModule SOMETHING
I need some like this: module One def test; puts 'Test One'; end end
Here is what my code looks like: module A def foo puts A end
Say I have this Ruby code in test.rb module MyModule class TestClassA end class
I`d like to know how I could unit-test the following module. def download_distribution(url, tempdir):
I have a maven project with a module /myProject pom.xml /myModule pom.xml /foo bar.txt
If I import a module defining a class of the same name belonging to
I am using python unit test module. I am wondering is there anyway to
When I test DAO module in JUnit, an obvious problem is: how to recover

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.