Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to understand sender and receiver in Ruby?

Tags:

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?

like image 868
OneZero Avatar asked Mar 23 '13 21:03

OneZero


1 Answers

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.

like image 62
dbenhur Avatar answered Oct 02 '22 22:10

dbenhur