I'm getting no method found error for my module methods. They are defined like so:
module MyModule
def my_method
"stuff"
end
end
And the class is like this:
class MyClass
include MyModule
def self.do_stuff
my_method
end
end
Then I'm trying to call it like so:
MyClass.do_stuff
I'm getting
undefined method 'my_method" for MyClass:Class
I have also tried
self.my_method
in the Module, but no luck. I should mention this is a vanilla ruby class within a rails application. application.rb includes the following line
config.autoload_paths += %W(#{config.root}/lib)
and in the console I can verify that the module is getting loaded. Any ideas on this? Thanks
you want to use extend to make the method a class method.
class MyClass
extend MyModule
def self.do_stuff
my_method
end
end
Define functions in module as module_function
and it should work.
module MyModule
module_function
def my_method
"stuff"
end
end
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.