顶层方法的定义 [英] Definition of method in top level

查看:62
本文介绍了顶层方法的定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道顶级方法的定义.

放置 RUBY_VERSION #=>2.1.5明确问候你好,世界"结尾类对象问候#=>你好,世界"结尾

为什么 greet 表现得像私有类方法?

放置 RUBY_VERSION #=>2.1.5明确问候早上好"结尾# 情况1Object.private_methods(false).grep/greet/#=>[]# 案例 2Object.private_instance_methods(false).grep/greet/#=>[:迎接]# 案例 3Object.private_methods(true).grep/greet/#=>[:迎接]

在情况 3 中,我发现 greet 是一个私有类方法.但我想知道哪个类拥有 greet 作为私有类方法.Object 继承自身?

编辑

我更新了我的问题.

问题 #1

方法的定义是否意味着在Object中添加一些方法作为私有实例方法.
这是正确的吗?

问题 #2

ObjectClass 的一个实例.所以,Object 拥有私有类方法.这些方法作为 Class.
中的私有实例方法这是正确的吗?

问题 #3

取决于问题 #1 和 #2.Class 继承了 Object.因此,Class 拥有 greet 作为私有类方法和私有实例方法.这是正确的吗?

解决方案

为什么 greet 表现得像私有类方法?

它没有.它就像一个私有实例方法.(实际上,Ruby 中没有类方法,只有实例方法.问题不是方法是实例方法还是类方法,而是实例方法定义在哪个类中.)

在顶层定义的方法成为Object的私有实例方法.

<块引用>

# case 1Object.private_methods(false).grep/greet/#=>[]# 案例 2Object.private_instance_methods(false).grep/greet/#=>[:迎接]# 案例 3Object.private_methods(true).grep/greet/#=>[:迎接]

在案例 3 中,我发现 greet 是一个私有类方法.

就像我上面说的:Ruby 中没有类方法.

greetObject 的私有实例方法.ObjectClass的实例,ClassModule的子类,ModuleObject 的子类,所以 ObjectObject 的实例,即它本身.

换句话说:Object 中定义的方法可用于所有对象.类也是对象,Object中定义的ergo方法适用于所有类,包括Object.

<块引用>

但我想知道哪个类拥有 greet 作为私有类方法.

没有.Ruby 中没有类方法.greetObject 的私有实例方法.

<块引用>

Object 继承自身?

没有.Object 自身的一个实例.

<块引用>

问题 #1

方法的定义是否意味着在Object中添加一些方法作为私有实例方法.
这是正确的吗?

在顶层定义的方法成为Object的私有实例方法.

<块引用>

问题 #2

ObjectClass 的一个实例.所以,Object 拥有私有类方法.这些方法作为 Class.
中的私有实例方法这是正确的吗?

我无法解析您的问题,抱歉.

但是,您的示例中没有 Class 的私有实例方法.您的示例中唯一的方法是 greet,它是 Object 的私有实例方法,而不是 Class.

如果你想知道谁拥有一个方法,你可以问Ruby:

method(:greet).owner# =>目的

<块引用>

问题 #3

取决于问题 #1 和 #2.Class 继承了 Object.因此,Class 拥有 >greet 作为私有类方法和私有实例方法.这是正确的吗?

没有.Ruby 中没有类方法,只有实例方法.而greet不属于Class,它是Object的私有实例方法:

method(:greet).owner# =>目的

I'm wondering about definition of method in top level.

puts RUBY_VERSION #=> 2.1.5
def greet
  "hello, world"
end
class Object
    greet #=> "hello, world"
end

Why does greet act like private class method?

puts RUBY_VERSION #=> 2.1.5
def greet
  "good morning"
end

# case 1
Object.private_methods(false).grep /greet/ #=> []
# case 2
Object.private_instance_methods(false).grep /greet/ #=> [:greet]
# case 3
Object.private_methods(true).grep /greet/ #=> [:greet]

In case 3, I found that greet is a private class method. But I'm wondering which class owns greet as a private class method. Object inherits itself?

Edit

I update my question.

Question #1

Does definition of method mean adding some methods in Object as private instance method.
Is this correct ?

Question #2

Object is an instance of Class. So, Object owns private class methods. These methods as private instance methods in Class.
Is this correct ?

Question #3

depends on question #1 and #2. Class inherits Object. So, Class owns greet as private class method and private instance method. Is this correct ?

解决方案

Why does greet act like private class method?

It doesn't. It acts like a private instance method. (In fact, there are no class methods in Ruby, only instance methods. The question is not whether a method is an instance method or a class method, rather it's what class the instance method is defined in.)

Methods defined at the top-level become private instance methods of Object.

# case 1
Object.private_methods(false).grep /greet/ #=> []
# case 2
Object.private_instance_methods(false).grep /greet/ #=> [:greet]
# case 3
Object.private_methods(true).grep /greet/ #=> [:greet]

In case 3, I found that greet is a private class method.

Like I said above: there are no class methods in Ruby.

greet is a private instance method of Object. Object is an instance of Class, Class is a subclass of Module, Module is a subclass of Object, ergo Object is an instance of Object i.e. itself.

Put another way: methods defined in Object are available for all objects. Classes are objects, too, ergo methods defined in Object are available for all classes, including Object.

But I'm wondering which class owns greet as a private class method.

None. There are no class methods in Ruby. greet is a private instance method of Object.

Object inherits itself?

No. Object is an instance of itself.

Question #1

Does definition of method mean adding some methods in Object as private instance method.
Is this correct ?

Methods defined at the top-level become private instance methods of Object.

Question #2

Object is an instance of Class. So, Object owns private class methods. These methods as private instance methods in Class.
Is this correct ?

I cannot parse your question, sorry.

However, there are no private instance methods of Class in your examples. The only method in your example is greet, which is a private instance method of Object, not Class.

If you want to know who owns a method, you can just ask Ruby:

method(:greet).owner
# => Object

Question #3

depends on question #1 and #2. Class inherits Object. So, Class owns >greet as private class method and private instance method. Is this correct ?

No. There are no class methods in Ruby, only instance methods. And greet is not owned by Class, it is a private instance method of Object:

method(:greet).owner
# => Object

这篇关于顶层方法的定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆