Ruby: module, require and include
Solution 1:
In short: you need to extend
instead of include
the module.
class MyApp
extend MyModule
self.hallo
end
include
provides instance methods for the class that mixes it in.
extend
provides class methods for the class that mixes it in.
Give this a read.
Solution 2:
The issue is that you are calling hallo
in the class definition, while you add it as an instance method (include
).
So you could either use extend
(hallo
would become a class method):
module MyModule
def hallo
puts "hallo"
end
end
class MyApp
extend MyModule
self.hallo
end
Or either call hallo
in an instance of MyApp:
module MyModule
def hallo
puts "hallo"
end
end
class MyApp
include MyModule
end
an_instance = MyApp.new
an_instance.hallo