[英]Overriding Instance Method in Rails Library
This seems like a monkey patch. 这看起来像是一个猴子补丁。 How can I improve it?
我怎样才能改进它?
Trying to override the deliver_now
instance method from the ActionMailer::MessageDelivery class. 尝试从ActionMailer :: MessageDelivery类重写
deliver_now
实例方法。 The below code works. 以下代码有效。
However, is there a way I can achieve this more elegantly say through class inheritance or some other way? 但是,有没有办法通过类继承或其他方式更优雅地实现这一点? The end code is used in a custom ActionMailer mailer.
结束代码用于自定义ActionMailer邮件程序。
module MyModule
class Ses
ActionMailer::MessageDelivery.class_eval do
def deliver_now!
puts self.to
puts self.subject
end
end
...
end
end
Note: I've seen these similar questions one & two but they do not address this issue. 注意:我已经看过这些类似的问题一和二,但他们没有解决这个问题。
You might look into Ruby's refinements 您可以查看Ruby的改进
An example refinement: 一个示例改进:
module MyMessageRefinement
refine(ActionMailer::MessageDelivery) do
def deliver_now!
puts self.to
puts self.subject
end
end
end
Then in your class: 然后在你的班上:
module MyModule
class Ses
using MyMessageRefinement
end
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.