2011-03-01 3 views
1

ruby ​​설명서의 module_function에있는 예제를 보았습니다. Mod.one이 오래된 "this is one"을 반환하고 c.one이 업데이트 된 "this is new one"을 반환하는 코드의 후반 부분을 이해하지 못합니다. 어떻게 이런 일이이 문서 Mod.one는 이전 코드 만 CLS 개체가 새 액세스 할 수 있습니다를 반환 왜ruby ​​설명서의 module_function 예제

module Mod 
    def one 
    "This is one" 
    end 
    module_function :one 
end 

class Cls 
    include Mod 
    def call_one 
    one 
    end 
end 

Mod.one  #=> "This is one" 
c = Cls.new 
c.call_one #=> "This is one" 

module Mod 
    def one 
    "This is the new one" 
    end 
end 

Mod.one  #=> "This is one" 
c.call_one #=> "This is the new one" 

에서 실제 코드입니다

일이 무엇입니까? 감사합니다. .

+1

'callOne'을'call_one'으로 편집하려고했는데, 당신이 문서에서 이와 같이 언급했다는 것을 제외하고는. 나는 이것에 대한 버그 보고서를 어떤 단계에서 제출할 것이다. –

+1

이제 수정되었습니다. http://redmine.ruby-lang.org/issues/4469 –

답변

4

module_function 실행은 다음의 코드와 일치, 즉, 모듈 레벨에서의 복사 기능을한다 :

module Mod 
    def Mod.one 
    "This is one" 
    end 

    def one 
    "This is the new one" 
    end 
end 

Mod.oneone과는 상이한 방법이다. 첫 번째 모듈은 어디서나 호출 할 수 있으며 두 번째 모듈은 클래스에 모듈을 포함 할 때 인스턴스 메소드가됩니다.

+0

설명해 주셔서 감사합니다. 매우 명확한 :) – felix