如何预先添加类方法

这个问题与此问题直接相关。 但我试图将其分解为基本问题,我不想在其他问题框中输入更多文本。 所以这里是:

我知道我可以通过扩展模块ClassMethods并通过Module #include钩子来包含classmethods。 但我可以在前置时做同样的事情吗? 这是我的例子:

Foo类:

class Foo def self.bar 'Base Bar!' end end 

类扩展:

 module Extensions module ClassMethods def bar 'Extended Bar!' end end def self.prepended(base) base.extend(ClassMethods) end end # prepend the extension Foo.send(:prepend, Extensions) 

class FooE:

 require './Foo' class FooE < Foo end 

和一个简单的启动:

 require 'pry' require './FooE' require './Extensions' puts FooE.bar 

当我启动脚本时,我没有得到Extended Bar! 像我期待的那样,而是Base Bar! 。 为了正常工作,我需要更改什么?

问题在于,即使您正在使用模块, ClassMethods仍然可以进行extend 。您可以这样做以获得您想要的:

 module Extensions module ClassMethods def bar 'Extended Bar!' end end def self.prepended(base) class << base prepend ClassMethods end end end 

请注意, Extensions本身可以预先添加或包含在Foo 。 重要的是预先添加ClassMethods

更简单的版本:

 module Extensions def bar 'Extended Bar!' end end Foo.singleton_class.prepend Extensions