通过混合模块来覆盖类的实例方法

给定A类和模块B,混合B的实例方法,使其覆盖A的相应实例方法。

module B def method1 "B\#method1" end def method2 "B\#method2" end end class A def method1 "A\#method1" end def method2 "A\#method2" end # include B does not override instance methods! # (module gets mixed into the superclass) end puts A.new.method1 # want it to print out "B#method1" puts A.new.method2 # want it to print out "B#method2" 

在包括B之前,您可以从A删除B中的每个方法。

 class A def method1 "A\#method1" end def method2 "A\#method2" end B.instance_methods(false).each { |method| remove_method(method) if instance_methods(false).include?(method) } include B end 

或者从B内:

 module B def method1 "B\#method1" end def method2 "B\#method2" end def self.append_features(mod) instance_methods(false).each { |method| mod.send(:remove_method, method) if mod.instance_methods(false).include?(method) } super end end 

Module#include #include将模块M作为C 类的超类插入。 所以,你不能在M覆盖C的方法,而是相反的方式: C的方法覆盖了M的方法。 (从技术上讲,Ruby不会使M成为C的超类,而是创建一个不可见的Include类 ⟦M′⟧其方法表和常量表指向M的方法表和常量表,并使该类成为超类,但这种区别对于这个特殊问题并不重要。)

在Ruby 2.0中,有一个新的方法, Module#prepend ,正如其名称所暗示的那样, MC的祖先,换句话说,使M成为C子类

所以,简而言之:你不能,至少还没有。