Ruby在没有显式接收器的情况下,什么类会获得方法? [英] Ruby what class gets a method when there is no explicit receiver?

查看:79
本文介绍了Ruby在没有显式接收器的情况下,什么类会获得方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

随机问题在这里.我不确定是否有一个术语,但是我想知道当您定义一个没有显式接收器的方法时,您怎么知道哪个类获取该方法?在这种情况下self是什么吗?

random question here. I'm not sure if there's a term for this, but I'm wondering when you define a method without an explicit receiver, how can you know what class gets the method? Is it whatever self is in that context?

self是正在定义的类,并且使用隐式接收器定义的方法已绑定到该类,我们一直都在看到它.

self in the context of a class definition is the class being defined, and methods defined with an implicit receiver are bound to the class, which we see all the time.

但是,如果我在实例方法中定义一个方法,那么'sub_method'也将被放在外部类中:

But, if I define a method inside an instance method, that 'sub_method' is getting put on the outer class as well:

[12] pry(main)> class A
[12] pry(main)*   def my_method
[12] pry(main)*     puts self
[12] pry(main)*     def sub_method
[12] pry(main)*       puts self
[12] pry(main)*     end  
[12] pry(main)*   end  
[12] pry(main)* end  
=> :my_method
[13] pry(main)> a = A.new
=> #<A:0x007fa588181d40>
[14] pry(main)> a.my_method
#<A:0x007fa588181d40>
=> :sub_method
[15] pry(main)> a.sub_method
#<A:0x007fa588181d40>
=> nil
[16] pry(main)> A.instance_methods(false)
=> [:my_method, :sub_method]

也在顶级范围内,selfmain,它是Object类的实例,但是在那里定义的方法被添加到Object中,而不是添加到main的单例类中,该类根据我对其他单例方法的工作方式的期望,这是我的期望:

Also at the top level scope, self is main, which is an instance of the Object class, but methods defined there are added to Object, not to mains singleton class, which is what I was expecting based on how I've seen other singleton methods work:

[21] pry(main)> def a.my_singleton
[21] pry(main)*   puts self
[21] pry(main)* end  
=> :my_singleton
[22] pry(main)> a.singleton_methods
=> [:my_singleton]
[23] pry(main)> A.instance_methods(false)
=> [:my_method, :sub_method]

[33] pry(main)> def why_object?
[33] pry(main)*   puts self
[33] pry(main)* end  
=> :why_object?
[34] pry(main)> why_object?
main
=> nil
[35] pry(main)> Object.instance_methods(false)
=> [:pry, :__binding__, :my_method, :sub_method, :why_object?]

这是怎么回事,是否有这方面的规定?还是只有这几种情况需要了解?

What is going on here, is there a rule to this or are there just these few cases to know about?

推荐答案

Ruby中有三个隐式上下文.

There are three implicit contexts in Ruby.

最著名的是self,它是当前对象和默认接收者.

The most well-known is self, the current object and the default receiver.

第二个众所周知的范围是用于常量查找的范围.广义上讲,不断查找是从字面上看向外,然后通过继承向上查找",但是有许多细微差别. Ruby维护人员将此上下文称为 cref .

The second well-known is the scope used for constant lookup. Broadly speaking, constant lookup is "lexically outward, then upward by inheritance", but there are many nuances. The Ruby maintainers call this context cref.

您要问的是第三个上下文,有时称为默认定义 .通常,默认定义是最近的词法包围模块.但是,您已经发现一个例外:在顶层,默认定义实际上是Object(加上,默认 visibility private). instance_eval更改self(更改为instance_eval消息的接收方)和默认定义(更改为接收方的 singleton类). class_eval都更改为接收者.

What you are asking about, is the third context, sometimes called the default definee. Usually, the default definee is the nearest lexically enclosing module. But, you already found one exception: at the top-level, the default definee is actually Object (plus, the default visibility is private). instance_eval changes both self (to the receiver of the instance_eval message) and the default definee (to the receiver's singleton class). class_eval changes both to the receiver.

这篇关于Ruby在没有显式接收器的情况下,什么类会获得方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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