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
Calling a module function from a class
c = MyClass.new c.test_module
Output 1:
$ ruby test_module.rb module_func_a invoked private_b invoked
Calling a module function on a module in the style of a class method
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 Output 1 and Output 2 when a module is included in a class, the problem does not arise when a private module method is called from a module function, while in the case of a direct call to a module function in a module in a class class method, a private module method called from a module function , not found.
Can someone make me understand the reason for the above behavior and is it possible to use a module function (which, in turn, calls a private module method) on a module in the style of a class method or not? If possible, what corrections are required in my code to do the same?
Jiggneshh gohel
source share