2011-03-08 233 views
0

有没有办法将“嵌套”嵌套模块,以便在扩展另一个类或模块时可以使用它们的所有方法?例如:扩展一个扩展类的模块

class User 
    extend UserStats 
end 

module UserStats 

    module Current 
    def active 
     where('status = "active"') 
    end 
    end 

end 

我希望能够延长UserStats(或用户),使得在UserStats ::目前的方法是可用的作为类方法对用户开放。

我试着在UserStats中“扩展当前”,但似乎没有工作。有没有办法做到这一点?

+0

当你说“似乎没有工作”,你是什么意思?你能给我们一个例子说明你如何使用它? – 2011-03-08 01:21:37

回答

4

为什么不只是extend UserStats::Current

+0

其实这将是罚款。我的情况的例子是一种元编程的老鼠窝,所以不是拼写出来,而是重新编辑原始问题并将其标记为答案。谢谢,我的道歉。 – 2011-03-08 01:30:04

+0

不需要道歉 - 我听说这是一个学习和讨论的地方! – 2011-03-08 02:36:07

1

你的意思是这样的吗?

module UserStats 

    def self.extended(klass) 
    klass.send(:extend, Current) 
    end 

    module Current 
    def active 
     puts "test" 
    end 
    end 

end 

class User 
    extend ::UserStats 
end 

puts User.active