about append_features method

原创文章,转载请注明来源并保留原文链接

在写上一篇关于concern的deprecated文章时,还发现两个有意思的东西,一并记录下来。

append_features

When this module is included in another, Ruby calls append_features in this module, passing it the receiving module in mod. Ruby’s default implementation is to add the constants, methods, and module variables of this module to mod if this module has not already been added to mod or one of its ancestors. See also Module#include.

我们知道extended和included是ruby的两个hook方法,原来include勾的就是这个append_features方法。

Module,Class的comparation

在`append_features`中有一句代码很简单,但却值得玩味:

def append_features(base)
  ...
  return false if base < self
  ...
end

原来module还可以比较:

mod <=> other_mod → -1, 0, +1, or nil

Comparison—Returns -1 if mod includes other_mod, 0 if mod is the same as other_mod, and +1 if mod is included by other_mod. Returns nil if mod has no relationship with other_mod or if other_mod is not a module.

但是

这里用来判断是否有继承关系,比较巧妙,如果base继承至self,说明方法已经在方法链上了,直接return false。

2 thoughts on “about append_features method

Leave a Reply

Your email address will not be published. Required fields are marked *