A
Adam Wilson
I have been having trouble today with a module include in a Rails app.
The module file sits in the /lib folder.
My problem, I worked out (which I have noticed before) is that I cannot
seem to access defined class methods in the module via the including
class. It seems that only instance methods are available. Let me
explain:
module MyModule
def self.test
"class"
end
def test
"instance"
end
end
class MyClass
include MyModule
end
class MyController
my_class = MyClass.new
test1 = MyClass.test #=> gives an NoMethodError
test2 = my_class.test #=> works
end
Is there something I am missing here? Please note I just wrote this code
quickly to show the idea, have not tested!
Its not essential but seems a bit silly that I need to use an instance
method in a situation when a class method would be much neater.
Any pointers much appreciated!
The module file sits in the /lib folder.
My problem, I worked out (which I have noticed before) is that I cannot
seem to access defined class methods in the module via the including
class. It seems that only instance methods are available. Let me
explain:
module MyModule
def self.test
"class"
end
def test
"instance"
end
end
class MyClass
include MyModule
end
class MyController
my_class = MyClass.new
test1 = MyClass.test #=> gives an NoMethodError
test2 = my_class.test #=> works
end
Is there something I am missing here? Please note I just wrote this code
quickly to show the idea, have not tested!
Its not essential but seems a bit silly that I need to use an instance
method in a situation when a class method would be much neater.
Any pointers much appreciated!