Ruby: module, require and include

时光毁灭记忆、已成空白 提交于 2019-11-29 22:06:21

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.

CCD

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

Your code is working - but including a module does not do what you think it does. The class including the module will not get the methods - the objects from this class will.

So this will work :

class MyApp
  include MyModule
end

my_app_object = MyApp.new
my_app_object.hallo # => hallo

my_app_object is an object of class MyApp, which has a mixins of module MyModule. Take a look there for a complete explanation of modules and mixins.

class MyApp
  class << self
    include MyModule
  end
  self.hallo
end

is the same as

class MyApp
  extend MyModule
  self.hallo
end

extends just opens the class object and include the module methods. "hallo" becomes a class object aka. static method of class MyApp.

So "include" inject the methods to the instances of the receiver, in your case being "self" NOT to the object itself. "extend" inject the methods to the receiver in your case being "self".

self.include MyModule // inject the methods into the instances of self
self.extend MyModule // inject the methods into object self

At class level "self" will point to your class object which is MyApp.

Also remember that "include" and "extend" are just methods defined in module.rb. "include" is a class object method (static-method) and "extend" is an instance method.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!