What does 'sender' / 'receiver' / 'message' mean in Ruby?

Consider the following:

a = "test123"

a.reverse # => '321tset' 

What is the sender / receiver / message in the above?

Pledge: By the end of this post, you will have the answer.

Consider this image:

_config.yml

** (my condolences if you are unable to see the colours. Notice carefully: black vs blue vs red.):

What is the message?

  • “reverse” is the message. you can think of messages as basically methods.

What is the receiver?

  • The string a is the receiver of the message.

What is the sender?

  • And the sender is the object where all of this is called. in this case, the object is self, which is main.

“Sending” a method

hello = "Namaskaram"
# => "Namaskaram"
hello.send(:reverse)
# => "maraksamaN"

How would you describe the above, in programming parlance?

  • We are calling the send message on hello.
  • reverse is sent on the hello object.
  • reverse is ‘invoked’ on hello.
  • reverse is called on the hello object.
  • reverse is the “caller” object.

Footnotes:

This was an answer I originally wrote (and modified) on StackOverflow.

Written on November 20, 2023