Ruby Basics : Sending a Message to a Receiver
In this article, you will learn about the receiver object and sending messages to a receiver.
Let's write a simple program to hail a taxi.
3.times do puts 'Taxi' end
Taxi Taxi Taxi
Receiver and Message
Let's identify the receiver and the message in this simple program. The number 3 is the receiver. The times() method is the message. The dot represents sending the message to the receiver.
The dot notation is used when sending a message is explicit.
The receiver is an object. Let's find out the class of the receiver object for our simple program.
This demonstrates that Fixnum object 3 is the receiver object. It receives the times() message.
Why is it called the receiver?
Because this object would have received the message that caused the method to execute.
Why do we need a receiver?
In a OO language like Ruby, everything happens by sending messages to an object. This object is the receiver. In the next chapter, we will discuss more about message passing.
Identify the receiver for the car program we wrote in the previous chapter.
In this article, we saw:
- What is a receiver?
- What is a message?
In the next article we will answer questions such as, what is a sender? Who is sending this message?
Ace the Technical Interview
- Easily find the gaps in your knowledge
- Get customized lessons based on where you are
- Take consistent action everyday
- Builtin accountability to keep you on track
- You will solve bigger problems over time
- Get the job of your dreams
Take the 30 Day Coding Skills Challenge
Gain confidence to attend the interview