‘Self’ in Ruby

Muhammad Ullah
3 min readNov 28, 2021

This blog intends to give more insight to beginners on what ‘Self’ is in Ruby and how it works in instance methods and class methods. When I first came upon learning ‘self’, it was very confusing to me because of the context it was in, but the more I practiced using ‘self’, the more I came to understand this special variable, the better I was at implementing it in different situations.

What is ‘Self’?

‘Self’ is a special variable that refers to the object that “owns” the executing code. One way to look at it is to ask yourself “Who am I?”. Who ‘self’ is, depends on where it is. It can be either in an instance method or a class method.

So far it sounds pretty obvious and it is, but it can become tricky trying to implement it, so we will go through some examples.

‘Self’ in an Instance Method

When creating a new instance of a class, that instance is now also known as an object. When you create an instance method and implement ‘self’, ‘self’ refers to that specific object/instance of that class.

Here is an example that has a Book class with each instance having attributes of ‘title’ and ‘author’. On line 14, a new instance/object has been created and set to the variable ‘newBook’ with a title=“Self” and author=“Mo”. On lines 9–11, there is a “title_author” instance method that puts out a sentence of “title by author”. You can also see that it uses ‘self’. Then on line 17, the new instance/object that was created calls the “title_author” instance method. Now ‘self’ in the instance method refers to that specific ‘newBook’ instance/object that calls the “title_author” instance method. The output is “Self by Mo” because ‘self.title=newBook.title’ and ‘self.author=newBook.author’. ‘Self’ belongs to whatever object/instance calls the instance method.

‘Self’ in a Class Method

When it comes to ‘self’ in a class method, ‘self’ refers to the entire class(not an instance/object of the class)

Book class that showcases the use of self in a class method.
Self is also being used in other areas of this example. Can you figure out what self is within the context? Remember to ask yourself “Who self is” based on where it is.

Here we are continuing with our Book class example. Notice anything different? On line 4 we have a class variable that is an array of all Book instances/objects. The array contains 3 three objects because three new instances were created from lines 17–19 and pushed into the array when the instance/object was initialized. Line 12 is where the class method is and you can tell it is a class method because “self.” comes before the name of the method. All class methods are defined as “ def self.class_method_name”. To reiterate, ‘self’ in the class method refers to the entire class. Self is not within an instance scope but the class scope. Our class method is defined as “self.all” which returns to us “@@all” variable which is an array of every instance of the Book class. So, when we called the class method on line 21, it outputs us that array.

Conclusion

So we’ve gone through what ‘self’ is and how it is used within an Instance Method and Class Method. There are more in-depth ways you can use ‘self’, but these are the basic examples. It might take some practice getting used to knowing what self is referring to in that context, but just look at where you are in the code and ask yourself “Who am I?”. It’s all about context.

--

--