相关疑难解决方法(0)

红宝石顶层定义的方法在哪里?

在顶层,方法定义应该导致私有方法开启Object,测试似乎证实了这一点:

def hello; "hello world"; end

Object.private_instance_methods.include?(:hello) #=> true
Object.new.send(:hello) #=> "hello world"
Run Code Online (Sandbox Code Playgroud)

但是,以下也适用于顶层(self.meta是本征类main):

self.meta.private_instance_methods(false).include?(:hello) #=> true
Run Code Online (Sandbox Code Playgroud)

似乎该hello方法同时在main和eigen的本征类上定义Object.这是怎么回事?请注意,该false参数用于private_instance_methods从方法列表中排除超类方法.

ruby singleton metaclass ruby-1.9

7
推荐指数
1
解决办法
2349
查看次数

标签 统计

metaclass ×1

ruby ×1

ruby-1.9 ×1

singleton ×1