简体   繁体   English

模块中的alias_method_chain

[英]alias_method_chain in module

I want a module to alias_method_chain a method from the class it is included into. 我希望模块从其包含的类中alias_method_chain一个方法。 Here is how I wrote it: 这是我写的:

module MyModule
  self.included(base)
    base.class_eval do
      alias_method_chain :perform, :chain
    end
  end
  def perform_with_chain(opts)
    #Do some stuffs
    perform_without_chain(opts)
    #Do some other stuffs
  end
end

class SomeClass
  include MyModule
  def perform(opts)
  end
end

but this throws an error since, when the module is included, the perform method is not yet defined in SomeClass : 但这会引发错误,因为当包含模块时, SomeClass尚未定义perform方法:

in `alias_method': undefined method `perform' for class `SomeClass' (NameError)

How should one write this pattern so the alias chain fully works? 一个人应该如何编写这种模式,以便别名链完全起作用?

Include after perform is defined. perform后定义包含。

class SomeClass
  def perform(opts)
  end
  include MyModule
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM