2014-05-22 23 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