Ruby:模块,需要和包含
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15097929/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Ruby: module, require and include
提问by Dakkar
I'm trying to use Ruby modules (mixins).
我正在尝试使用 Ruby 模块(mixins)。
I have test.rb:
我有test.rb:
#!/usr/bin/env ruby
require_relative 'lib/mymodule'
class MyApp
include MyModule
self.hallo
end
and lib/mymodule.rb:
和 lib/mymodule.rb:
module MyModule
def hallo
puts "hallo"
end
end
Quite simple setup. But it does not work :( :
很简单的设置。但它不起作用:(:
ruby test.rb
test.rb:8:in `<class:MyApp>': undefined method `hallo' for MyApp:Class (NoMethodError)
from test.rb:6:in `<main>'
Where is my error?
我的错误在哪里?
回答by Kyle
In short: you need to extendinstead of includethe module.
简而言之:您需要extend而不是include模块。
class MyApp
extend MyModule
self.hallo
end
includeprovides instance methods for the class that mixes it in.
include为混合它的类提供实例方法。
extendprovides class methods for the class that mixes it in.
extend为混合它的类提供类方法。
Give thisa read.
给这个读一读。
回答by CCD
The issue is that you are calling halloin the class definition, while you add it as an instance method (include).
问题是您hallo在类定义中调用,同时将其添加为实例方法 ( include)。
So you could either use extend(hallowould become a class method):
所以你可以使用extend(hallo将成为一个类方法):
module MyModule
def hallo
puts "hallo"
end
end
class MyApp
extend MyModule
self.hallo
end
Or either call halloin an instance of MyApp:
或者调用halloMyApp 的一个实例:
module MyModule
def hallo
puts "hallo"
end
end
class MyApp
include MyModule
end
an_instance = MyApp.new
an_instance.hallo
回答by Martin
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 therefor a complete explanation of modules and mixins.
my_app_object 是类 MyApp 的对象,它具有模块 MyModule 的混合。看看那里有关于模块和混合的完整解释。
回答by Viorel
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.
extends 只是打开类对象并包含模块方法。“hallo”成为一个类对象。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.
在类级别“self”将指向您的类对象,即 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.
还要记住,“include”和“extend”只是module.rb中定义的方法。“include”是一个类对象方法(静态方法),“extend”是一个实例方法。

