2011-09-18 104 views
14

我讀到ActiveSupport :: Concern的API。有ClassMethodsInstanceMethods,我們可以把類方法放在ClassMethods爲什麼我們需要ClassMethods和InstanceMethods?

但是M的主機可以使用M中定義的方法,不是嗎?爲什麼我不能只是寫:

module M 
    def self.x 
    end 

    def y 
    end 
end 

而不是:

module M 
    module ClassMethods 
    def x 
    end 
    end 
    module InstanceMethods 
    def y 
    end 
    end 
end 

回答

11

您可能會感興趣Yehuda's take on this pattern。我認爲其中一些原因是歷史性的,因爲除非您手動執行通過includeextend自動執行的操作,否則它們並不是真正需要的。

1

依賴的悉心照料。請參閱提供的示例here