Ruby 对自我的定义
我正在读一本 Ruby 书,发现了伪变量 self 的定义:
self - 当前的接收者对象 方法
可以分解这个定义并解释它的含义吗? 我一点也不明白。
编辑:实际上,我很清楚 self 是什么(及其应用程序),并且我知道如何在 Google 上搜索。 我只是想知道是否有人可以解释我引用的定义。 具体来说就是。
I was reading a Ruby book and came across this definition of the pseudo-variable self:
self - receiver object of the current
method
Could someone break down that definition and explain what it means? I don't understand any of it.
EDIT: I actually have a pretty good idea of what self is (and its applications) and I know how to search on Google. I was just wondering if someone could explain the definition I quoted. That specifically.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Ruby 和其他语言(例如 Smalltalk 和 Objective-C)更喜欢术语“消息传递”,而 Java 和 C++ 更喜欢“方法调用”。 也就是说,“Java 方式”是调用对象的方法(在对象的上下文中运行代码),而“Ruby 方式”是向对象发送消息,对象通过运行其方法来响应该消息。
Ruby 将
my_string.length
行描述为“发送my_string
thelength
消息”。my_string
接收消息,因此称为接收者; 在length
方法的定义中,self
将引用my_string
。 您可以使用my_string.send(:length)
获得相同的效果。从消息传递的角度思考这个概念比从方法调用的角度思考这个概念更灵活。 要调用对象上的方法,该方法必须已预先定义,而您可以向对象发送一条消息,该对象可以选择动态处理该消息(使用
respond_to?
和method_missing
代码>)。 这种灵活性是允许 Ruby 用作简洁的领域特定语言 (DSL) 的一个方面。Ruby and other languages (such as Smalltalk and Objective-C) prefer the term "message passing", whereas Java and C++ prefer "method invocation". That is, the "Java way" is to call a method on an object — running code in the context of an object — whereas the "Ruby way" is to send an object a message, to which the object responds by running its method.
Ruby would describe the line
my_string.length
as "sendingmy_string
thelength
message". Themy_string
receives the message, and so is called the receiver; inside the definition of thelength
method,self
would refer tomy_string
. You can get the same effect withmy_string.send(:length)
.Thinking of this concept in terms of message passing is more flexible than thinking in terms of method invocation. To invoke a method on an object, that method must have been pre-defined, whereas you can send an object a message that it can choose to handle dynamically (with
respond_to?
andmethod_missing
). This flexibility is one aspect that allows Ruby to be used as concise domain-specific languages (DSL).self
是一个特殊变量,它会根据上下文而变化。 更具体地说,正如您提到的,它是当前方法的接收者对象。 要理解这一点,我们需要理解接收器的含义。请参阅Ruby 编程:有关方法的更多信息 和 类和对象。
在
foo.twoTimes
中,foo
部分被称为方法调用的接收者。因此,在
twoTimes
方法中,self
引用上下文中的对象foo
。这里还有一个很好的解释
self
is a special variable that changes depending on the context. To be more specific, it is receiver object of the current method, as you mentioned. To understand this, we need to understand what receiver means.See Programming Ruby: More About Methods and Classes and Objects.
In
foo.twoTimes
,foo
part is called the receiver of the method call.So, within the
twoTimes
method,self
refers to the objectfoo
in the context.There is also a very good explanation here
Ruby 中的“方法调用”是通过消息发送机制来完成的。 So
是一个简写,
我认为这就是引用所指的内容:“self”是消息(或方法)已发送到的对象:当前方法的接收者。
整个消息发送过程是 Ruby 如此动态的一部分。 它使对象可以轻松地为它当前未处理的消息定义
method_missing
并决定如何处理它们。 Rails 经常使用它:例如,ActiveRecord 具有“find_by...”语法,它可以从调用/发送的方法名称中找出需要的内容。"Method calling" in Ruby is accomplished through a message-sending mechanism. So
is a shorthand for
I think this is what the quote is referring to: "self" is the object to which the message (or method) has been sent: the receiver of the current method.
The whole message-sending thing is part of what makes Ruby so dynamic. It makes it easy for an object to define
method_missing
for messages it doesn't currently handle and decide what to do with them. Rails uses this a lot: ActiveRecord, for example has the "find_by..." syntax, which figures out what's wanted from the name of the method called/sent.