对于理解Ruby的方法查找过程,祖先链概念非常重要,相同模块在原祖先链的类中多次混入时,新祖先链如何呢,做了一个小试验:
?
module M; end module M1; end module M2; end class MyClass include M include M1 end class MySubClass < MyClass include M include M2 end obj = MySubClass.new puts obj.class.ancestors() puts "\n\n\n" puts BasicObject.superclass==nil def my_ancestors(current_class) a = [] a.push current_class sc = current_class.superclass until sc==nil a.push sc sc = sc.superclass end a end puts my_ancestors(obj.class)