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.
This “current object” depends on the context.
Yes, the context is where your code is at any given moment.
Here’s an example:
You can see this in action yourself.
Run this code:
def coffee puts self end coffee # main
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:
class Cat def meow puts self end end Cat.new.meow # <Cat:0x7a14c5>
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:
class Example def do_something banana = "variable" puts banana puts self.banana end def banana "method" end end Example.new.do_something # "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:
class Salad def self.buy_olive_oil # ... end end Salad.buy_olive_oil
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:
def ==(other) self == other end
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.
class Salad def initialize @ingredients =  end def add_nuts @ingredients << :nuts self end end 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,nil].select(&:itself) # [1, 2 ,3]
This example filters
Understanding the difference:
selfis a keyword. It's value depends on where you use it
itselfis a method. It's value depends on what object you use it on (in
1is 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! 🙂