Ruby 如何 alias 一个类方法或者模块方法呢?

deathking · 2012年07月03日 · 最后由 DeathKing 回复于 2012年07月19日 · 3447 次阅读
module Foo

  def self.bar
    "bar"
  end

  alias foobar bar  #=> undefined bar method
  alias self.foobar self.bar #=> Syntax Error

end

可以用 alias 来操作模块方法么?

class << self
  alias foobar bar
end

现在没环境。不知道这样行不行?

#1 楼 @jjym 可以

module Foo

  class << self 
  def bar
    "bar"
  end

   alias foobar bar

  end

end

#2 楼 @chucai 我都把这件事给忘了……今天登录才发现还有这件事。确实可行,谢了。

需要 登录 后方可回复, 如果你还没有账号请 注册新账号