2011-10-08 88 views
3

凡在Rubinius的源是負責包括模塊的代碼?(具體放置模塊作爲超類對象類的。)Rubinius在哪裏實現了mixin?

+0

恐怕它被放置在vm/builtin/*中,並寫在cpp中。 –

+1

你低估了Rubinius的冷靜:-) –

回答

8

如果你看一下Module#include的文檔,你會發現它代表Module#append_features

對每個參數以相反的順序調用Module.append_features

Module#append_features的文檔,依次介紹了(很簡單),默認的Ruby的mixin算法如何工作的:

當該模塊包含在另一個,Ruby調用此模塊中append_features,傳遞接收模塊mod。 Ruby的默認實現是將此模塊的常量,方法和模塊變量添加到mod,前提是此模塊尚未添加到mod或其某個祖先。另見Module#include

如果您在Rubinius sourcecodeModule#append_features,你會發現這是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) 
    ... 

您的具體問題:

中準確地放置模塊作爲超類對象類

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

+0

偉大的anwser!謝謝! –