我们已经知道 module中的inciude 关键字可以把模块中的方法嵌入为类中的实例方法,当然方法的作用域可以理解为继承父类,extend关键字可以把模块中的方法嵌入为类中的类方法。
除此之外,module中 有时候也需要嵌套其他模块,这时候就需要用到关键字included 和self 关键字 和base 关键字。代码如下:
module A
def self.included base
base.include B
base.extend C
end
module B
def hi
puts "This is B module"
end
end
module C
def hello
puts "This is C module"
end
end
end
class ABC
include A
end
a = ABC.new
a.hi
ABC.hello
输出结果:
This is B module
This is C module