“Self ” in Ruby

Yuri Nguyen
4 min readMar 29, 2021

Like many other students, when I first started my coding journey at the Flatiron SE program, I couldn’t fully wrap my head around the use of “self”. I could see it appeared on multiple lines of code, but wasn’t sure what values its held. Well, it’s true that “The more you practice, the better you get”, (and “My life would suck without you Binding.pry” so thank youuu) I am more comfortable using “self” now compared to 5 weeks ago. In this blog I will explain what “self” is from my understanding.

What is Self ???

Think of “self” as Who Am I right now ???

Simply put: “self” is referring to the object itself. Depending on where you use self, it can refer to different things. Let’s take a look at some examples:

Instance Method:

What is self on line 8 & 9?

On line 8 & 9, self is used inside an instance method so they both refer to the instance. However on line 9, we explicitly use self to get access to breed. Without attr_accessor( or attr_reader/attr_writer)or a getter method, I will get an error: undefined method “breed”.

Having said that, I can also get the same result like line 9 by looking at attr_accessor to get the breed or directly accessing instance_variable @breed (line 10 & 11).

Class Method:

What is self on line 12 & 13?

On line 12, self is used to create a class method so we will need to use the class to access it and in this case it’s Dog. What is a class method you might ask? A class method is defined beginning with self: “def self.a_method; end”. On line 13, because self is used inside a class method so it refers to the class itself.

If you try to call golden.second_who_am_i, you will get an error undefined method “second_who_am_i” because again “def self.second_who_am_i; end” is a class method, not an instance method.

Let me give you another example so you can have a better understanding about the use of class method

How am I able to get all the animals at once ? In case I want to get another animal such as: dog = Animal.new(“dog”, “Molly”), is there a way I can keep track of all of animals, group them together ? You probably guess it right: by evoking class method “def self.all” on line 14. If it’s still unclear to you then let’s me break it down step by step:

  1. Assign class variable @@animals = [ ] (line 4)
  2. Whenever a new animal is created, we need to push that animal instance into class variable @@animals. We create a new animal called bear (line 20) so the instance in this case is bear so self also refers to bear, same thing apply to line 21 when new lion is created, the instance in this case is lion so self refers to lion.

3. It takes us to the last step, put @@animals inside our class method “def self.all” on line 14.

You might ask why we need to call class variable @@animals inside class method “def self.all” if @@animals itself is already an array holding all animals we have created. Can’t we just call @@animals directly ?? The answer is NO, not outside of the class Animal. The class method “def self.all” on line 14 is actually a getter method helping us to read outside of the class Animal , whose job is to return @@animals value.

Last example I swear… Outside of any Class:

Since def Flower doesn't belong to any class so it is a top-level object. The self here refers to main: an instance of Object.

Andddd that's it from me. I hope I could help you get a better understanding about “self”. Please leave a comment if I make any mistakes in the blog and see you all next time ;)

--

--

Yuri Nguyen

Student at Flatiron School. A traveler. A bubble tea aficionado.