我正在查看ruby mixin博客文章,它说当一个模块包含在一个类中时,它的self.included()方法被调用。
我的问题是,这种行为在哪里正式记录?我似乎无法在 ruby-docs.org 网站或镐上找到它。
我正在查看ruby mixin博客文章,它说当一个模块包含在一个类中时,它的self.included()方法被调用。
我的问题是,这种行为在哪里正式记录?我似乎无法在 ruby-docs.org 网站或镐上找到它。
While it's not on Ruby Doc for some reason, included actually is documented. Running ri Module.included in the terminal provides this:
included( othermod )
Callback invoked whenever the receiver is included in another module or class. This should be used in preference to
Module.append_featuresif your code wants to perform some action when a module is included in another.module A def A.included(mod) puts "#{self} included in #{mod}" end end module Enumerable include A end
This documentation can be found in the Ruby source in object.c. Sadly, Module.extended is not documented.
我怀疑它不在 RubyDoc 网站上,因为它是一个私有方法,并且私有方法当前没有显示。
人们意识到了这个问题,但他们还没有弄清楚如何处理私有方法,即使它们不是实现细节。
两者都记录在 pickaxe 第二版的第 556 页(涵盖 Ruby 1.8)。那里的文档看起来就像ri Module.includedAndrew Marshall 发布的结果,所以我怀疑这本书的部分是自动生成的。如果它已从更高版本的 pickaxe 中删除,则可能是由于相同的错误导致它无法显示在ruby-doc.org上。
似乎只记录了公共方法