2

鉴于此模块

module Test
  def self.foo(v)
    puts "Test.foo with #{v}"
  end
end

以下不起作用

module Test  
  alias_method :bar, :foo
  # ...
end

虽然它适用于实例方法。我收到以下错误

NameError: undefined method `foo' for module `Test'

我的目标是覆盖self.foo如下

def self.foo(v)
  self.bar(v + " monkey patched")
end

有没有别名静态方法?

谢谢,

4

1 回答 1

2
Test.singleton_class.send(:alias_method, :bar, :foo)
Test.bar("cat")
  #=> "Test Foo with cat"
于 2017-06-15T22:34:03.047 回答