Ruby Language Message Passing Introduction


In Object Oriented Design, objects receive messages and reply to them. In Ruby, sending a message is calling a method and result of that method is the reply.

In Ruby message passing is dynamic. When a message arrives rather than knowing exactly how to reply to it Ruby uses a predefined set of rules to find a method that can reply to it. We can use these rules to interrupt and reply to the message, send it to another object or modify it among other actions.

Each time an object receives a message Ruby checks:

  1. If this object has a singleton class and it can reply to this message.
  2. Looks up this object's class then class' ancestors chain.
  3. One by one checks if a method is available on this ancestor and moves up the chain.