当 class 覆盖它时调用 Object#send

Call Object#send when a class override it

我有一个 class MyServer < UNIXServer 继承自 UNIXServer

我需要打电话给 Object#send method on its object, but I don't know how to call it because UNIXServer override it with this method。有什么想法吗?

医生说:

You can use __send__ if the name send clashes with an existing method in obj.

我想拥有原始的 send 方法,您可以在 Object class.

中创建一个别名
# Opening the `Object` class
# and aliasing the original `send
Object.class_eval do
  alias_method :original_send, :send
end

执行此操作后,您可以在 MyServer 实例中调用 original_send,它的行为类似于原始 send 方法。

如果这对您来说比较抽象,请查看 Metaprogramming in Ruby


正如 Jörg W Mittag 和@pioz 所说,您也可以使用 __send__ 方法。 Docs