Ruby Basics : The Default Receiver

In this article, you will learn that when you don't provide an explicit receiver in the code, Ruby uses self as the default receiver object.

The self Within Car

Let's define a Car class and print the value of self inside the Car class.

class Car
  puts self
end

This prints:

Car

Car

The drive Class Method

Let's define a drive() class method in the Car class.

class Car
  def self.drive
    p 'driving'
  end
end

We know that the value of self is Car. So, this is the same as:

class Car
  def Car.drive
    p 'driving'
  end
end

Call the Class Method

Explicit Receiver

We can call the drive() class method inside the Car class.

class Car
  def self.drive
    p 'driving'
  end

  Car.drive
end

This prints:

driving

We can also use self instead of Car to call the drive() class method.

class Car
  def self.drive
    p 'driving'
  end

  self.drive
end

This also prints:

driving

Call the Class Method

No Receiver

We know the value of self inside the Car class is Car. We can omit the self from the above example.

class Car
  def self.drive
    p 'driving'
  end

  drive
end

This prints:

driving

We don't have the dot notation that sends a message or the receiver object. The message is sent to the current value of self whenever you call a method with no receiver. Since the current value of self is Car, we are able to call the drive class method. In this example, Car is the default receiver object.

Fabio Asks

Why is it called default receiver?

Because, when you don't provide an explicit receiver, the current value of self defaults as the receiver.

Rhonda Asks

Why do we need a receiver to send a message?

In a OO language like Ruby, all messages are sent to some object.

Insight

There is another way to think about this example. You can say that whenever the sender object and the receiver object is the same, you can omit the receiver.

Same Sender and Receiver

Car is Sender and Receiver

We will revisit this idea in an upcoming article.

Subclass Calling Class Method

The example may seem trivial, but this allows us to write code like this:

class Car
  def self.drive
    p 'driving'
  end
end

class Beetle < Car
  drive
end 

This example prints:

driving

Rails Example

This is like ActiveRecord library in Rails. Imagine that ActiveRecord find() implementation is like this:

class ActiveRecord
  def self.find(id)
    p "Find record with id : #{id}"
  end
end

In our web application we have a Beetle subclass of ActiveRecord as the model.

class Beetle < ActiveRecord
end 

Beetle

In the controller, we use the find method:

Beetle.find(1)

This prints:

"Find record with id : 1"

The dot notation makes sending messages explicit. If the receiver and the sender is the same, you can omit the receiver and the dot. In this case, Ruby will use value of self as the receiver. Thus, self is the default receiver.

Summary

In this article, you learned that the message is sent to the current value of self when you call a method with no receiver. This object is called the default receiver.


Related Articles


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

No spam ever. Unsubscribe anytime.