Rubinius 中的 mixin 在哪里实现?

发布于 2024-12-09 01:52:06 字数 53 浏览 0 评论 0原文

Rubinius源代码中的哪里是负责包含模块的代码?(具体来说,将模块放置为对象类的超类。)

Where in the Rubinius source is the code that is responsible for including modules?(Specifically, to place module as super class of object class.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

2024-12-16 01:52:06

如果您查看 Module#include 的文档,您会发现它委托给 Module#append_features

以相反的顺序对每个参数调用 Module.append_features

Module#append_features 的文档又(非常简要地)描述了如何默认的 Ruby mixin 算法工作原理:

当此模块包含在另一个模块中时,Ruby 会在此模块中调用 append_features,并将其传递给 mod 中的接收模块。如果此模块尚未添加到 mod 或其祖先之一,则 Ruby 的默认实现是将该模块的常量、方法和模块变量添加到 mod 中。另请参阅Module#include

如果您查看 Module#append_featuresRubinius 源代码中,你会发现它是 Module#include_into 的别名:

# 当此模块包含在另一个模块中时调用。
# 这可能会被自定义行为覆盖。默认
# 是添加常量、实例方法和模块变量
# 该模块以及该模块包含到 +klass+ 的所有模块。
#
# 另请参阅#include。
#
alias_method :append_features, :include_into

所以,最后, Module#include_into 是真正的交易:

# 添加所有常量、实例方法和模块变量
该模块的 # 以及该模块包含到 +klass+ 的所有模块
#
# 该方法的别名为append_features,作为默认实现
# 对于该方法。 kernel#extend直接通过调用该方法
# Module#extend_object,因为 Kernel#extend 不应该使用append_features。
def include_into(类)
  ...

您的具体问题:

正是将模块放置为对象类的超类

这个循环< /a>:

k = klass.direct_superclass
而 k
  如果 k.kind_of? Rubinius::包含模块
    #哦,我们找到了。
    如果 k == mod
      # 好吧,如果我们仍在直接包含的模块中
      # of klass,然后把未来的东西放在mod后面,而不是放在
      # 开始。
      insert_at = k 除非 superclass_seen
      添加=假
      休息
    结尾
  别的
    superclass_seen = true
  结尾

  k = k.direct_superclass
结尾

留意 insert_at

If you look at the documentation for Module#include, you’ll find that it delegates to Module#append_features:

Invokes Module.append_features on each parameter in reverse order.

The documentation for Module#append_features, in turn, describes (very briefly) how the default Ruby mixin algorithm works:

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.

If you look at Module#append_features in the Rubinius sourcecode, you’ll find that it is an alias for Module#include_into:

# Called when this Module is being included in another Module.
# This may be overridden for custom behaviour. The default
# is to add constants, instance methods and module variables
# of this Module and all Modules that this one includes to +klass+.
#
# See also #include.
#
alias_method :append_features, :include_into

So, finally, Module#include_into is the real deal:

# Add all constants, instance methods and module variables
# of this Module and all Modules that this one includes to +klass+
#
# This method is aliased as append_features as the default implementation
# for that method. Kernel#extend calls this method directly through
# Module#extend_object, because Kernel#extend should not use append_features.
def include_into(klass)
  ...

Your specific question:

exactly to place module as super class of object class

is answered in this loop:

k = klass.direct_superclass
while k
  if k.kind_of? Rubinius::IncludedModule
    # Oh, we found it.
    if k == mod
      # ok, if we're still within the directly included modules
      # of klass, then put future things after mod, not at the
      # beginning.
      insert_at = k unless superclass_seen
      add = false
      break
    end
  else
    superclass_seen = true
  end

  k = k.direct_superclass
end

Watch for insert_at.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文