Ruby 请教个 Ruby 的基础问题

lululau · April 20, 2012 · Last by zw963 replied at April 23, 2012 · 2683 hits

方法“attr_accessor”和“module_function”同样都是类"Module"的实例方法,但是为什么“attr_accessor”可以在定义类的时候这样用:

class Hello
  attr_accessor :hello
end


而“module_function”却不可以这样用呢:

class Hello
  def hello
  end

  module_function :hello
end


我觉得唯一合理的解释就是类“Class”在继承类“Module”的时候,把方法“module_function”给删除了?

Class 是把 module_function 给 undef 掉了。

#1 楼 @skandhas 谢谢,知道了还有 undef_method 这种东西

没看过源代码。不过猜测就是 skandhas 说的那样。 正如其名,这个方法只是在模块类中被使用才有意义

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