在 Ruby 中,“接收者”做什么? 参考?

发布于 2024-07-21 20:51:45 字数 37 浏览 5 评论 0原文

我正在阅读一份关于具有接收器的方法的文档。 什么是接收器?

I'm reading a document that talks about a method having a receiver. What's a receiver?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

咆哮 2024-07-28 20:51:45

在 Ruby(以及受 SmallTalk 启发的其他语言)中,对象被认为是发送和接收“消息”。

在 Ruby 中,Object 作为一切事物的基类,有一个 send 方法: Object.send 例如:

class Klass
  def hello
    "Hello!"
  end
end
k = Klass.new
k.send :hello     #=> "Hello!"
k.hello           #=> "Hello!"

在这两种情况下,k 都是“hello”消息的接收者

In Ruby (and other languages that take inspiration from SmallTalk) objects are thought of as sending and receiving 'messages'.

In Ruby, Object, the base class of everything, has a send method: Object.send For example:

class Klass
  def hello
    "Hello!"
  end
end
k = Klass.new
k.send :hello     #=> "Hello!"
k.hello           #=> "Hello!"

In both of these cases k is the receiver of the 'hello' message.

浅浅 2024-07-28 20:51:45

在最初的 Smalltalk 术语中,“对象”上的方法被称为对象消息(即,您没有调用对象 foo 上的方法,而是向对象 foo 发送了一条消息)。 所以 foo.blah 正在发送“blah”消息,“foo”对象正在接收该消息; “foo”是“blah”的接收者。

In the original Smalltalk terminology, methods on "objects" were instead refered to as messages to objects (i.e. you didn't call a method on object foo, you sent object foo a message). So foo.blah is sending the "blah" message, which the "foo" object is receiving; "foo" is the receiver of "blah".

ぶ宁プ宁ぶ 2024-07-28 20:51:45

之前的对象。

将调用方法 xy 视为“将指令 y 发送到对象 x”。

这是smalltalk 的思维方式,当您了解Ruby 的一些更高级的功能时,它会对您很有帮助。

the object before the .

think of calling a method x.y as saying "send instruction y to object x".

it's the smalltalk way of thinking, it will serve you well as you get to some of Ruby's more advanced features.

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