# How self Keyword Works in Ruby

*Published on November 04, 2022*

---


Everything in Ruby is an object. The Ruby interpreter runs every line of code you write in the context of an object.&nbsp;**The**&nbsp;**`self`**&nbsp;**keyword points to this object.**

Here are the four important rules to keep in mind with the&nbsp;`self`&nbsp;keyword:

- Only one object can be&nbsp;`self`&nbsp;at a given time.
- `self`&nbsp;is constantly changing as a program executes.
- When you call a method, the receiver of that method (i.e. the object on which you called the method) becomes&nbsp;`self`.
- All methods without an explicit receiver are called on&nbsp;`self`.

Here are the different forms&nbsp;`self`&nbsp;can take, depending on where it's used.

![ruby_self.webp](https://writesoftwarewell.com/media/xtbehjz8ke239mv5l9l17lqz2mje.webp "Different forms of the self keyword")

## Top-Level

At the top level,&nbsp;`self`&nbsp;points to the&nbsp;`main`&nbsp;object, which is an instance of&nbsp;`Object`&nbsp;class.

As soon as a Ruby program starts, the Ruby interpreter creates an object called&nbsp;`main`, and all subsequent code is executed in the context of this object.

```ruby
puts self  # main
puts self.class  # Object
```

This context is also called top-level context.

## Inside a Class or Module

Ruby lets you run any code inside the class or module definition, where the role of&nbsp;`self`&nbsp;is taken by the class or module itself.

```ruby
class Language
  puts self  # Language
end

module Maths
  puts self # Maths
end
```

## Inside a Class Method

The class method runs in the context of the class, not its instance. The class itself '_owns_' that method.

In a class method,&nbsp;_`self`_&nbsp;points to the class.

```ruby
class Language  
  def self.run
    puts self  # Language
  end
end
```

It works similarly for a module's class methods.

```ruby
module Maths
  def self.calculate
    puts self  # Maths 
  end
end
```

## Inside an Instance Method

All instance methods belong to the instance. Hence,&nbsp;`self`&nbsp;points to the specific instance of the class.

```ruby
class Language
  def compile
    puts self  # #<Language:0x00007fc7c191c9f0>
  end
end

ruby = Language.new
ruby.compile
```

When you call a method, Ruby looks up the method in the object's ancestor chain and then executes the method with the receiver as&nbsp;`self`.

## Inside a Module (Mixin) Method

Modules are one of Ruby's coolest features. They're somewhat similar to classes: they hold methods. However, you can't instantiate them directly. Modules let you mix these methods into another class (hence the word mixin💡).

When you include a module into a class, i.e. (mix in the module's methods as instance methods), the&nbsp;`self`&nbsp;object in that method points to the instance of that class.

```ruby
module Task
  def execute
    puts self  # #<Project:0x0000000107fdce30>
  end
end

class Project
  include Task
end

p = Project.new
p.execute
```

When you extend a module into a class, i.e. (mix in the module's methods as class methods), the&nbsp;`self`&nbsp;object in that method points to the class.

```ruby
module Task
  def execute
    puts self # Client 
  end
end

class Client
  extend Task
end

Client.execute
```

So that was a brief overview of how the&nbsp;`self`&nbsp;keyword works in Ruby. Understanding it will give you new powers to wield in your Rails apps.

I hope you found this article useful and that you learned something new. If you have any questions or feedback, please&nbsp;[send me an email](mailto:akshay.khot@hey.com). I look forward to hearing from you.

Please subscribe to my blog below if you'd like to receive future articles directly in your email. If you're already a subscriber, THANK YOU 🙏

