---
title: Understanding `self` in Ruby
published: "2015-11-02"
publisher: Honeybadger
author: Starr Horne
category: Ruby articles
tags:
  - Ruby
description: Many problems faced by beginning Rubyists are caused by not understanding self. Join me and take a deep dive into self in Ruby under common and uncommon conditions.
url: "https://www.honeybadger.io/blog/ruby-self-cheat-sheet/"
---

Today I'd like to talk about `self` in Ruby. If you've been programming Ruby for a while, you've likely internalized the idea of `self`. Whenever you read or write a program, `self` is there in the back of your mind.

But for less-experienced Rubyists, `self` can be baffling. It's always changing, but it's never explicitly shown in the code. You're just expected to know.

A lot of the problems beginners face are caused by not understanding `self`. If you've ever "lost" an instance variable or puzzled over what data is visible to a mixin, then it's because you didn't understand `self` in that context.

![Ruby self confusion](https://www-files.honeybadger.io/posts/ruby-self-cheat-sheet/ruby-self.jpg)

In this post, we're going to look at the Ruby `self` keyword in a variety of everyday situations.

## What is `self`?

You may have heard people say that [everything in Ruby is an object](https://www.honeybadger.io/blog/ruby-object-model/). If that's true, it means that every piece of code you write "belongs" to some object.

`self` is a special variable that points to the object that "owns" the currently executing code. Ruby uses `self` everywhere:

- For instance variables: `@myvar`
- For method and constant lookup
- When defining methods, classes, and [modules](https://www.honeybadger.io/blog/avoid-these-traps-when-nesting-ruby-modules/).

In theory, `self` is pretty obvious. But in practice, it's easy for tricky situations to pop up. That's why I wrote this post.

## Examples of `self` in Ruby

We're going to step through several examples now. If the first ones seem too basic for you, just keep reading. They get more advanced.

### `self` inside of an instance method

In the code below, `reflect` is an instance method. It belongs to the object we created via `Ghost.new`. So, `self` points to that object.

```ruby
class Ghost def reflect self end end g = Ghost.new g.reflect == g # => true
```

### `self` inside of a class method

For this example, `reflect` is a class method of `Ghost`. In this case, the class itself "owns" the method. `self` points to the class.

```ruby
class Ghost def self.reflect self end end Ghost.reflect == Ghost # => true
```

It works the same with "class" methods inside of modules. For example:

```ruby
module Ghost def self.reflect self end end Ghost.reflect == Ghost # => true
```

Remember, classes and modules are treated as objects in Ruby. So, this behavior isn't that different from the instance method behavior we saw in the first example.

### `self` inside of a class or module definition

One feature of Ruby that makes it such a good fit for frameworks like Rails is that you can execute arbitrary code inside class and module definitions. When you put code inside of a class/module definition, it runs just like any other Ruby code. The only real difference is the value of `self`.

As you can see below, `self` points to the class or module that's in the process of being defined.

```ruby
class Ghost self == Ghost # => true end module Mummy self == Mummy # => true end
```

### `self` inside mixin methods

Mixed-in methods behave just like "normal" instance or class methods when it comes to `self`. This makes sense. Otherwise, the mixin wouldn't be able to interact with the class you mixed it into.

#### Instance methods

Even though the `reflect` method was defined in the module, its `self` is the instance of the class it was mixed into.

```ruby
module Reflection def reflect self end end class Ghost include Reflection end g = Ghost.new g.reflect == g # => true
```

#### Class methods

When we `extend` a class to mix in class methods, `self` behaves exactly like it does in normal class methods.

```ruby
module Reflection def reflect self end end class Ghost extend Reflection end Ghost.reflect == Ghost # => true
```

### `self` inside the metaclass

Chances are you've seen this popular shortcut for defining lots of class methods at once.

```ruby
class Ghost class << self def method1 end def method2 end end end
```

The `class << foo` syntax is actually pretty interesting. It lets you access an object's metaclass - which is also called the "singleton class" or "eigenclass". The term "singleton class" is very popular these days. But for now, you just need to know that the metaclass is where Ruby stores methods that are unique to a specific object.

If you access `self` from inside the `class << foo` block, you get the metaclass.

```ruby
class << "test" puts self.inspect end # => #<Class:#<String:0x007f8de283bd88>
```

### `self` outside of any class

If you're running code outside of any class, Ruby still provides `self`. It points to "main", which is an instance of `Object`:

```ruby
puts self.inspect # => main
```

## What understanding `self` does for you

If you're just starting out, understanding `self` in Ruby will allow you to really understand the code you are writing. The real beauty of Ruby's design is that once you grasp this concept, code that previously seemed magical becomes logical and predictable.

If you're ever debugging Ruby code and encountering unexpected behavior with instance variables or method visibility, ask yourself, "What is self right now?" More often than not, this simple question will continue to serve you well, no matter how advanced your Ruby programming becomes.

---

## Try Honeybadger for FREE

Intelligent logging, error tracking, and Just Enough APM™ in one dev-friendly platform. Find and fix problems before users notice.

[Start free trial](https://app.honeybadger.io/users/sign_up)

[See plans and pricing](https://www.honeybadger.io/plans/)
