2014-05-22 24 views
3

我想要做這樣的事情:包括一個混合內混入

module Mixin 
    def self.included(base) 
    base.include AnotherMixin 
    ... 
    end 
end 

這給錯誤

NoMethodError - private method `include' called for Class 

我如何可以包括一個mixin一個mixin在裏面使用我正在定義的方法?

+0

只是刪除從'base.include AnotherMixin'了'base' ...它將工作。 –

+0

@ArupRakshit - 它將包含AnotherMixin withn Mixin - 嘗試使用'included_modules'檢查它 – BroiSatse

+0

@BroiSatse *如何在mixin中包含mixin *意味着什麼? –

回答

3

如何在mixin中包含mixin以用於我定義的方法?

隨着錯誤消息明確告訴你,#include私人方法,所以明確的接收器是不允許的,在Ruby中。因此,要完成此操作,只需從方法調用#include中刪除base即可。它將包含模塊mixin內的模塊AnotherMixin。現在,正在調用#include,由隱含的self調用,它已被設置爲模塊對象Mixin

下面將做的工作: -

module Mixin 
    def self.included(base) 
    include AnotherMixin 
    #... 
    end 
end