有没有办法在 Ruby 中获取所有方法的别名?

ica*_*oli 5 ruby metaprogramming

假设我有一堂课:

class MyClass
  def my_method
    # cool stuff
  end
  alias :my_method2 :method
end
Run Code Online (Sandbox Code Playgroud)

现在我想获取方法 my_method 的所有别名,而不与所有对象方法进行比较。

Mar*_*mas 3

我不知道如何在不使用比较的情况下做到这一点。但是,如果删除 Object.methods,则可以限制进行的比较:

def aliased?(x)
  (methods - Object.methods).each do |m|
    next if m.to_s == x.to_s
    return true if method(m.to_sym) == method(x.to_sym)
  end
  false
end 
Run Code Online (Sandbox Code Playgroud)