Ruby Basics : The self at the Top Level

In this article, you will learn about the current object, self at the top level.

What is self?

In Ruby, there is always one object that plays the role of current object at any given instant. The current object provides an execution context for the code.

This current object is the default receiver. When the receiver is not provided, the message is sent to the default receiver. This is the self.

What is Self

Self at the Top Level

Let's see the value of self at the top level.

puts self

This prints:

main

This tells us that Ruby has created an object called main for us at the top level. And all the code we write at the top level will use main as the receiver in method calls.

Self at the Top Level

The main Object

If main is an object, it must be instance of some class. We can ask Ruby to tell us which class main is an instance of:

puts self.class

This prints:

Object

We now know, Ruby did something like this:

main = Object.new

This provides us an object context to execute our code at the top level. Thus, providing us the default receiver main at the top level. The main is an instance of the Object class.

Hello at the Top Level

Let's print hello to the standard output.

puts "hello"

As expected, this prints hello.

Top Level Print Hello

Is main a Receiver Object?

Can we use main, the instance of Object, to call puts?

main.puts 'hi'

We get:

NameError: undefined local variable or method main for main:Object

Ruby prints main as the current object at the top level. But, there is no such variable called main.

Human Visible main Object

The main is the human visible representation of the current object. Let's see this in action in the IRB console.

 > self
 => main 
 > self.inspect
 => "main" 
 > self.to_s
 => "main" 

Fabio Asks

Why does Ruby create main object at the top level?

In a OO language like Ruby, there is always a sender and receiver involved in a message send. We did not explicitly create a sender object. Thus, Ruby created a sender object for us. It is implicit, because it is not visible in the code.

Same Sender and Receiver

In an upcoming article, we will see that both sender and receiver are main at the top level.

Rhonda Asks

If I don't create a receiver object, does Ruby create a receiver object?

No. Ruby does not create a receiver object; it uses the existing value of self as the default receiver.

Code Executes in Current Object

Summary

In this article, we discussed the default receiver self at the top level context. You learned that we cannot specify an explicit receiver for methods in the top level like puts(). In such cases, the receiver is implicit and is not specified. We will see why in an upcoming article.


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.