假设存在以下代码:
module Foo def test(x) "Foo #{x}" end end module Bar def test(x) "Bar #{x} " + super end end class C include Foo include Bar end puts C.new.test(2) # => "Bar 2 Foo 2"
我无法访问C类代码,也无法访问模块Foo和Bar.
我想在Foo和Bar之间包含一个模块,这样:
module Between def test(x) "Between " + super end end puts C.new.test(2) # => "Bar 2 Between Foo 2"
这是如何实现的?
解决方法
module Bar; include Between end class C; include Bar end puts C.new.test(2) #=> Bar 2 Between Foo 2
但请注意,在实践中,应避免使用这种无意义的模块杂耍.