只是让我的头在Ruby元编程。 mixin /模块总是会让我困惑。Ruby中的include和extend有什么区别?
- 包括:在指定的模块的方法混合在目标类
- 实例方法延伸:在指定的模块的方法混合在目标类类方法
那么这个主要区别就在这个还是一个更大的潜伏龙? 例如
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!"
查看这个链接:http://juixe.com/techknow/index.php/2006/06/15/mixins-in-ruby/ – Donato 2016-12-20 17:53:00