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

deathking · July 03, 2012 · Last by DeathKing replied at July 19, 2012 · 3449 hits
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 我都把这件事给忘了……今天登录才发现还有这件事。确实可行,谢了。

You need to Sign in before reply, if you don't have an account, please Sign up first.