增强Ruby方法

在我的控件库中有一个方法,它使用块来做很棒的事情。现在,我想创建一些重载,让我的生活更轻松,但无法弄清楚如何让它像我想的那样工作。 这是我到目前为止所拥有的:
class ClassNotDefinedByMe
  def enhanced_awesome_method(&block)
    if block.arity < 2
      awesome_method(&block)
    else
      # Doing my own extensions here.
      # This part is not the issue.
    end
  end
end
ClassNotDefinedByMe
在lib中,
awesome_method
也是如此。
awesome_method
接受一个块并为该块执行0或1参数的有意义的事情。任何其他数量的参数都会导致引发异常。我写了上面的内容,增加了在块中使用2或3个参数的可能性,一切正常,但现在我必须调用
enhanced_awesome_method
而不是
awesome_method
。 更多说明的例子(x是
ClassNotDefinedByMe
的一个例子):
# These two comes from the library:
x.awesome_method{     puts "works" }
x.awesome_method{ |i| puts "also works, the argument is #{i}" }

# Now I can do this:
x.enhanced_awesome_method{ |i,j|   puts "oh, two arguments! cool." }
x.enhanced_awesome_method{ |i,j,k| puts "three arguments, how bold!" }

# But I want to be able to do this:
x.awesome_method{         puts "still works" }
x.awesome_method{ |i|     puts "still works" }
x.awesome_method{ |i,j|   puts "still works" }
x.awesome_method{ |i,j,k| puts "still works" }
有人可能会争辩说,我必须为我的增强方法使用一个新名称是一件好事,因此很明显它是一种不同的方法,但我真的想用我自己的方法替换
awesome_method
。它不会破坏任何现有代码,因为0和1参数情况的行为是相同的,并且这些是迄今为止唯一合法的情况。 所以,我遇到的问题是,如果我将我的方法命名为
awesome_method
并将其传递给具有0或1参数的块,它将永远调用自身。如果这是继承的情况,我会通过调用ѭ10来解决它,但在这种情况下,当我只是改变了类本身时,我该怎么办?     
已邀请:
使用
alias
关键字来创建原始函数的别名,然后您可以覆盖调用别名方法的方法。 例如:
class ClassNotDefinedByMe
    alias :old_awesome_method :awesome_method

    def awesome_method(&block)
        if block.arity < 2
            old_awesome_method(&block)
        else
            # Awesome stuff here
        end
    end
end
    
您尝试执行的操作称为方法别名,但它确实替换了原始方法。     

要回复问题请先登录注册