Self, @, @@, <<, [ ] in Ruby

YU SHI
5 min readFeb 17, 2021

--

While learning Ruby programming language you may find the use of “self” very confusing, but it’s important to know how to use “self” in Ruby. Also you will encounter some symbols @, @@, <<, [ ]. You might want to know what all these symbols mean in Ruby.

In this article, we are going to explore the following things:

  • @, @@, <<, [ ]
  • Self

What is @, @@, <<, [ ]?

I am going to use one of the group lab from the bootcamp to explain what the “self” and @, @@, <<, [ ] are in the instance method and class method. Before we dive into self, you should already know how to create classes, class methods, instance methods, etc.

First, let’s see what each of these symbols (@, @@, <<, [ ]) mean in this lab.

  • @(@name, @department, @age) is an instance variable of the class, meaning scope the variable to the instance.
  • @@(@@all) is a class variable, meaning that @@ variables are in scope of the class and all the instances.
  • The << (@@all << self) operator is adding all the Manager instances to the result array. Here, we see that the “self” is all the instances of Manager.
  • [ ] operator is used here (@@all = [ ]) to set our class variable equal to an empty array. Arrays are for storing lists of data, so we’ll use an array to store our lists of Manager instances.

As you can see, all the methods we wrote in the image above worked. When we test the “Manager.all”, it returns an array of all the Manager instances(@name, @department, @age) that have been initialized. The “Manager.all” is able to return an array of all the Manager instances because we have the class method(def self.all) that reads the value of the variable. Again here(self.all), the “self” is the Manager class.

Want to read this story later? Save it in Journal.

If we don’t create a class method for Manager class, it will return “NoMethodError”.

If we don’t have “@@all << self” inside the initialize method, it will return an empty array when we test “Manager.all”.

Also we can show you what happens if we remove an instance variable. Let’s say we remove the instance variable “@name = name”. As you can see the result below, the instance variable @name is not there anymore. It’s because @name is not in the scope of the class.

Who is the “self” in this method?

The “self” is a Ruby keyword that gives you access to the current object. Everything in Ruby is an object. String(“”), array([ ]), integer(1), range(0..3), and ted(name) are objects. Even classes themselves are objects. Every object has its own identity and data. Ted has its own identity(Manager) and data(“Ted”, “Accounting”, “35”).

Here, I am going to show you how to find out who is “self” in each method by using the pry. In the image below, there are an instance method #employees and class method self.all_department.

We are going to put the binding.pry inside the instance method #employees to see who is the “self” here. Make sure to put the require ‘pry’ on top of the class. We all don’t like to see errors.

Inside the employees method, “self” is the instance of the Manager yu. In this “#employees” instance method, it’s selecting all the employees that the manager oversees.

Now, we are going to put the binding.pry inside the class method “Manager.all_department” to see who is the self. Self is the Manager. In this method, it returns an array of all the department names that every manager oversees without repetitions. We have to put .uniq in the end of the method because we don’t want repetitions. When you look back to the manager instances, you will see there are two managers in the accounting department.

Why do we use self?

We are using “self” because we don’t want to use the class name for each method definition. Self makes our code easier to change if we change the class. And it makes the code less noisy and easy to read. We don’t want to use “manager.all_department” because it’s too long. That’s why we use “self.all_department”.

Self was hard for me to understand in the beginning, but it was very important to know what “self” is in the method. I hope this article helps everyone understand what self and @, @@, <<, [ ] do in Ruby. If you think I explained something wrong or unclear, please leave a message in the comment.

📝 Save this story in Journal.

--

--

YU SHI

Software Engineer experienced in JavaScript, React, and Ruby on Rails based programming. Currently looking for an opportunity.