在 Ruby 中,“接收者”指的是什麼? (In Ruby what does the "receiver" refer to?)


問題描述

在 Ruby 中,“接收者”指的是什麼? (In Ruby what does the "receiver" refer to?)

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


參考解法

方法 1:

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.

方法 2:

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".

方法 3:

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.

(by lorzCameron PopeAdam Wrightchillitom)

參考文件

  1. In Ruby what does the "receiver" refer to? (CC BY-SA 3.0/4.0)

#terminology #ruby






相關問題

抽象 ViewModel 在被繼承時是否被視為模型? (Is an abstract ViewModel considered a Model when it is inherited?)

什麼是 Lambda? (What is a Lambda?)

具體確定項目順序的排序的正確術語是什麼? (What is the proper term for an ordering where the order of items is concretely determined?)

在 Ruby 中,“接收者”指的是什麼? (In Ruby what does the "receiver" refer to?)

錯誤跟踪和問題跟踪系統有什麼區別? (What's the difference between a bug tracking and an issue tracking system?)

為什麼術語 API 和 SDK 似乎可以互換使用? (Why do the terms API and an SDK seem to be used interchangeably?)

Java 中的對等類是什麼? (What is a peer class in Java?)

模擬和模擬有什麼區別? (what is the difference between Emulate and Simulate?)

協議術語:消息與數據包 (Protocol Terminology: Message versus Packet)

表示“目錄”或“文件”的詞是什麼? (What is the word that means "directory" or "file"?)

C ++中復合語句和塊之間的區別? (Difference between a compound statement and a block in C++?)

gnu八度中gnu的含義? (Meaning of gnu in gnu octave?)







留言討論