当一个类include多个模块,而这些模块中又有同名的方法时,类中调用的会是哪个模块的方法呢?先看下面的代码: 

module Printable
         def print
                   #...
         end
         def prepare_cover
                   #...
         end
end
module Document
         def print_to_screen
                   prepare_cover
                   format_for_screen
                   print
         end
         def format_for_screen
                   #...
         end
         def print
                   #...
         end
end
class Book
         include Document
         include Printable
         #...
end
b = Book.new
b.print_to_screen

你能猜出是哪个版本的print()方法被调用了么?是Printable类的还是Document类中的?
我们可以通过询问Ruby得到Book的祖先链:

Book.ancestors   #=> [Book, Printable,Document,Object,Kernel,BasicObject]

当Book包含Document模块时,Ruby为Document模块创建一个包含类,并把它加入到Book的祖先链上;再一次,Ruby为Printable模块创建一个包含类,并把它加入到Book的祖先链上,位置紧挨着Book,这样,从Document往上的其他成员,则顺次提高一位。
当调用b.print_to_screen方法时,对象b成为self,并且开始进行方法查找。Ruby在Document模块中找到了print_to_screen()方法,并且这个方法还调用了其他方法,其中就包括print(),所有没有明确指明接收者的调用都会作用于self,因此又开始从Book类(self所属于的类)开始方法查找,在祖先链中最近一处定义print()方法的是Printable#print(),因此这个方法被调用了。