If you’re learning Ruby you may find the use of the “self” keyword very confusing.
How does it work?
It’s a Ruby keyword that gives you access to the current object.
If you don’t know what objects are, watch this video I made for you. It’s related to Object-Oriented Programming.
This “current object” depends on the context.
Yes, the context is where your code is at any given moment.
Here’s an example:
If your code is inside an instance method,
self is an instance of that class. In other words,
self is an object.
You can see this in action yourself.
Run this code:
This code defines & calls a
coffee method which prints the value of
Why does it print
Because it’s the name of the top-level object, it’s an object where you’ll find all the methods defined outside a class.
coffee method here.
But if you define a method inside a class named
self would be a
As seen here:
From these examples, we can tell that the value of
self changes depending on where you use it.
I believe you can understand anything better by understanding its purpose.
Let’s look at some examples.
Using Self For Disambiguation
One practical use for
self is to be able to tell the difference between a method & a local variable.
It’s not a great idea to name a variable & a method the same. But if you have to work with that situation, then you’ll be able to call the method with
Here’s what I mean:
banana = "variable"
# "variable" => puts banana
# "method" => puts self.banana
Here we have a
banana local variable, inside the
do_something method, but we also have a
A local variable takes priority.
That’s why we need to use
self here if we want to call the
banana method, instead of printing the value of the
Let’s look at a more common use case… Defining class methods!
Using Self To Define Class-Level Methods
Number ONE use for
self, without a doubt, is to define class-level methods.
I’m sure you’ve seen them.
def self.method_name method definitions.
Like this one:
self here is equivalent to
Salad, the class name.
Why is this useful?
Because we don’t have to use the class name for each method definition, making our code easier to change if we change the class.
It also makes the code less noisy & better to read.
That’s why we do
def self.buy_olive_oil instead of
Other Uses For Self
There are even more uses for
self, so it’s a helpful thing to be familiar with.
You can use it in a method to compare the current object with another object.
Something like this:
self == other
You can also use
self for debugging purposes, to find out which object you’re working with.
Or you can use it as a return value, to implement design patterns like the Builder Design Pattern.
@ingredients = 
@ingredients << :nuts
my_salad = Salad.new.add_nuts
In summary, here's a list of helpful uses for self:
- Define class-level methods
- Use an instance method when you have a local variable of the same name
- Returning Self (builder pattern)
- Comparing objects (==)
- Default receiver of method calls
Self vs Itself
One more thing we should look at before we are done with this topic.
It's a method that you can call on an object & the object will return itself.
# [1, 2 ,3]
This example filters
Understanding the difference:
self is a keyword. It's value depends on where you use it
itself is a method. It's value depends on what object you use it on (in
1 is the object)
Hope that's helpful.
You have learned about Ruby self keyword, exactly what it is, why it's useful & how to use it.
Now it's your turn to give it a try.
Thanks for reading! 🙂
arharhahahrr, I finally understand self, great guide! :), this one had me confused so many times