2011-10-08 5 views
3

는 Rubinius 소스에서 모듈 등을 담당하는 코드는 어디? (특히, 객체 클래스의 슈퍼 클래스로 모듈을 배치 할 수 있습니다.)Rubinius에서 mixins은 어디에 구현 되었습니까?

+0

나는 vm/builtin/*에 두려워하고 cpp로 작성됩니다. –

+1

당신은 Rubinius의 차가움을 과소 평가합니다 :-) –

답변

8

당신이 Module#include에 대한 설명서를 보면, 당신은 찾을 수 있습니다 그것은 위임자는 Module#append_features :

역순으로 각 매개 변수에 Module.append_features을 호출합니다. 이 모듈은 다른에 포함되는 경우

은 루비를 통과,이 모듈 append_features 호출

Module#append_features에 대한 문서, 차례로, 기본 루비 믹스 인 알고리즘이 작동하는 방법 (매우 간략하게) 설명 수신 모듈은 mod입니다. 루비의 기본 구현은이 모듈이 mod이나 조상 중 하나에 아직 추가되지 않았다면이 모듈의 상수, 메소드 및 모듈 변수를 mod에 추가하는 것입니다. Module#include도 참조하십시오.

# 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이 실제 거래는 다음과 같습니다 :

당신이 Rubinius sourcecodeModule#append_features 보면

, 당신은 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에 대한 답변 개체 클래스의 슈퍼 클래스로 모듈을 배치 : insert_at에 대한

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 

시계.

+0

위대한 anwser! 감사! –

관련 문제