我只是在了解Ruby元编程。mixin/modules总是把我弄糊涂。
Include:在目标类中混合指定的模块方法作为实例方法
扩展:在目标类中混合指定的模块方法作为类方法
所以主要的区别是这一点,还是有更大的龙潜伏着?
如。
module ReusableModule
def module_method
puts "Module Method: Hi there!"
end
end
class ClassThatIncludes
include ReusableModule
end
class ClassThatExtends
extend ReusableModule
end
puts "Include"
ClassThatIncludes.new.module_method # "Module Method: Hi there!"
puts "Extend"
ClassThatExtends.module_method # "Module Method: Hi there!"
当您将模块包含到类中时,模块方法将作为实例方法导入。
但是,当您将模块扩展为类时,模块方法将作为类方法导入。
例如,如果我们有一个模块Module_test,定义如下:
module Module_test
def func
puts "M - in module"
end
end
现在,对于include模块。如果我们这样定义A类:
class A
include Module_test
end
a = A.new
a.func
输出将是:M - in模块。
如果我们将include Module_test替换为extend Module_test并再次运行代码,我们会收到以下错误:undefined method 'func' for #<A:instance_num> (NoMethodError)。
将方法调用a.func更改为a.func,输出更改为:M - in模块。
从上面的代码执行可以清楚地看出,当我们包含一个模块时,它的方法就变成了实例方法,而当我们扩展一个模块时,它的方法就变成了类方法。
所有其他的答案都很好,包括挖掘RubySpecs的提示:
https://github.com/rubyspec/rubyspec/blob/master/core/module/include_spec.rb
https://github.com/rubyspec/rubyspec/blob/master/core/module/extend_object_spec.rb
至于用例:
如果在类ClassThatIncludes中包含模块ReusableModule,则会引用方法、常量、类、子模块和其他声明。
如果你用模块ReusableModule扩展类ClassThatExtends,那么方法和常量就会被复制。显然,如果不小心,动态复制定义可能会浪费大量内存。
如果你使用ActiveSupport::Concern, .included()函数可以让你直接重写include类。关注点中的模块ClassMethods被扩展(复制)到包含类中。
扩展——将指定模块的方法和常量添加到目标的元类(即单例类)
如。
如果你调用Klazz.extend(Mod),现在Klazz有Mod的方法(作为类方法)
如果你调用obj.extend(Mod),现在obj有Mod的方法(作为实例方法),但obj.class的其他实例没有添加这些方法。
Extend是一个公共方法
include——默认情况下,它将指定模块的方法作为目标模块/类的实例方法混合在一起。
如。
如果你称班级为Klazz;包括国防部;,现在Klazz的所有实例都可以访问Mod的方法(作为实例方法)
Include是一个私有方法,因为它打算从容器类/模块内部调用。
然而,模块经常通过对所包含的方法进行猴子补丁来覆盖include的行为。这在遗留Rails代码中非常突出。Yehuda Katz报道。
关于include的更多详细信息及其默认行为,假设您已经运行了以下代码
class Klazz
include Mod
end
If Mod is already included in Klazz, or one of its ancestors, the include statement has no effect
It also includes Mod's constants in Klazz, as long as they don't clash
It gives Klazz access to Mod's module variables, e.g. @@foo or @@bar
raises ArgumentError if there are cyclic includes
Attaches the module as the caller's immediate ancestor (i.e. It adds Mod to Klazz.ancestors, but Mod is not added to the chain of Klazz.superclass.superclass.superclass. So, calling super in Klazz#foo will check for Mod#foo before checking to Klazz's real superclass's foo method. See the RubySpec for details.).
当然,ruby核心文档始终是实现这些功能的最佳途径。RubySpec项目也是一个极好的资源,因为他们精确地记录了功能。
包括RubySpec rubydoc
包括RubySpec rubydoc
扩展RubySpec rubydoc
扩展的RubySpec rubydoc
#extend_object RubySpec rubydoc
# append_featuresrubyspec rubydoc