gpt4 book ai didi

ruby - Rubinius 中的 mixins 在哪里实现?

转载 作者:数据小太阳 更新时间:2023-10-29 07:47:01 26 4
gpt4 key购买 nike

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

最佳答案

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

Invokes Module.append_features on each parameter in reverse order.

Module#append_features 的文档反过来,(非常简短地)描述了默认的 Ruby mixin 算法是如何工作的:

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.

如果你看Module#append_featuresRubinius sourcecode ,你会发现它是 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

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

# 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)
...

您的具体问题:

exactly to place module as super class of object class

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

注意 insert_at .

关于ruby - Rubinius 中的 mixins 在哪里实现?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7695431/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com