在顶层,方法定义应该导致私有方法开启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从方法列表中排除超类方法.