Ruby 对自我的定义

发布于 2024-07-20 19:04:07 字数 232 浏览 7 评论 0原文

我正在读一本 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

沉睡月亮 2024-07-27 19:04:07

Ruby 和其他语言(例如 Smalltalk 和 Objective-C)更喜欢术语“消息传递”,而 Java 和 C++ 更喜欢“方法调用”。 也就是说,“Java 方式”是调用对象的方法(在对象的上下文中运行代码),而“Ruby 方式”是向对象发送消息,对象通过运行其方法来响应该消息。

Ruby 将 my_string.length 行描述为“发送 my_string the length 消息”。 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 "sending my_string the length message". The my_string receives the message, and so is called the receiver; inside the definition of the length method, self would refer to my_string. You can get the same effect with my_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? and method_missing). This flexibility is one aspect that allows Ruby to be used as concise domain-specific languages (DSL).

江心雾 2024-07-27 19:04:07

self 是一个特殊变量,它会根据上下文而变化。 更具体地说,正如您提到的,它是当前方法的接收者对象。 要理解这一点,我们需要理解接收器的含义。

请参阅Ruby 编程有关方法的更多信息类和对象

通过指定一个方法来调用一个方法
接收者,方法的名称,以及
可选的一些参数和
关联块。

connection.downloadMP3("jitterbug") { |p| showProgress(p) }

在此示例中,对象connection
是接收器,downloadMP3
方法的名称,"jitterbug"
参数,以及之间的东西
大括号是关联的块。

foo = "hello"
bar = foo.dup
class <<foo
  def to_s
    "The value is '#{self}'"
  end
  def twoTimes
    self + self
  end
end

foo.to_s        »   "The value is 'hello'"
foo.twoTimes    »   "hellohello"
bar.to_s        »   "hello"

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.

You call a method by specifying a
receiver, the name of the method, and
optionally some parameters and an
associated block.

connection.downloadMP3("jitterbug") { |p| showProgress(p) }

In this example, the object connection
is the receiver, downloadMP3 is the
name of the method, "jitterbug" is the
parameter, and the stuff between the
braces is the associated block.

foo = "hello"
bar = foo.dup
class <<foo
  def to_s
    "The value is '#{self}'"
  end
  def twoTimes
    self + self
  end
end

foo.to_s        »   "The value is 'hello'"
foo.twoTimes    »   "hellohello"
bar.to_s        »   "hello"

In foo.twoTimes, foo part is called the receiver of the method call.
So, within the twoTimes method, self refers to the object foo in the context.

There is also a very good explanation here

书间行客 2024-07-27 19:04:07

self - 当前的接收者对象
方法

Ruby 中的“方法调用”是通过消息发送机制来完成的。 So

some_object.some_method(args)

是一个简写,

some_object.send(:some_method, args)

我认为这就是引用所指的内容:“self”是消息(或方法)已发送到的对象:当前方法的接收者。

整个消息发送过程是 Ruby 如此动态的一部分。 它使对象可以轻松地为它当前未处理的消息定义 method_missing 并决定如何处理它们。 Rails 经常使用它:例如,ActiveRecord 具有“find_by...”语法,它可以从调用/发送的方法名称中找出需要的内容。

self - receiver object of the current
method

"Method calling" in Ruby is accomplished through a message-sending mechanism. So

some_object.some_method(args)

is a shorthand for

some_object.send(:some_method, args)

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文