OneZero OneZero - 2 months ago 20
Ruby Question

How to understand sender and receiver in Ruby?

I find it hard to understand the actual meaning of sender and receiver in Ruby. What do they mean in general? So far I simply understand them as the method call and the one that takes its return value. However, I know my understanding is far from enough. Can anyone give me an specific explanation of sender and receiver in Ruby?

Answer

A core concept in Object Orientation is messaging and early conceptualization borrowed much from the Actor Model of computation. Alan Kay, the guy who coined the term Object Oriented and invented one of the first OO languages SmallTalk, has voiced regret at using a term which put the focus on objects instead of on messages, which he considered the stronger idea.

When talking about a message, there's a natural "sender" and "receiver" of the message. The sender is the object which invokes a method, the receiver is the object whose method is invoked. In Ruby, if one calls a method without explicitly naming an object, that sends the method name and its args as a message to the default receiver self.

In OO, "making a call", "invoking a method", and "sending a message" are equivalent concepts. Similarly "being called", "having one's method invoked", and "receiving a message" are equivalent.

Comments