Ruby: alias_method 对于模块静态方法
Ruby: alias_method for module static method
鉴于此模块
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
有没有away to alias static方法?
谢谢,
Test.singleton_class.send(:alias_method, :bar, :foo)
Test.bar("cat")
#=> "Test Foo with cat"
鉴于此模块
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
有没有away to alias static方法?
谢谢,
Test.singleton_class.send(:alias_method, :bar, :foo)
Test.bar("cat")
#=> "Test Foo with cat"