Where are mixins implemented in Rubinius?

社会主义新天地 提交于 2019-12-07 18:14:31

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/7695431/where-are-mixins-implemented-in-rubinius

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!