How to self-educate in self in Ruby
Congratulations! This is probably your 1023983576th encounter on an article about self in Ruby. self is a very confusing topic, and so are methods and variables… Well, the list goes on! Worry not, I am here to help you self-educate. Let’s focus on these three things:
- What is self?
- When do we use self?
- Why do we use self?
What is self?
The keyword self is important to understand because self translates to other languages as well. In Javascript there is a keyword this, and self is the Ruby version of Javascript’s this. Ruby is an object oriented programming language. Every time we execute, we deal with some type of object whether a Class or an Instance ( a copy or a new instance of a class). self is going to reference one of these two objects.
self is a special variable that refers to an object that owns the current code that is being executed.
Because it is a variable, we must be aware of the scope of self. We need to determine who owns the executing code by asking where we define self, either in Class or Instance. In addition, the scope will never change for self. Scope being where something is defined, A class scope begins with class, ends with end. Every scope begins with def and end. Anything else is default execution
- Inside of an instance method, self will represent an instance.
In the code below, mirror is an instance method. It belongs to the object we created Human.new. Therefore self points to that object, leslie. - Inside a class method, self will reference the class itself. The receiver of the method is the class itself . In the code below, mirror is a class method of Human. Class, Human, owns the method mirror and self, points to the class.
class Human def mirror #instance method
self
end leslie = Human.new
leslie.mirror #=> <Human:0x00007f939a38f948> #object we created
def self.mirror #class method
self
end
Human.mirror == Human #=> trueend
When do we use self?
As we saw above, we can use self when inside of an instance method, defining a class method. Before we move forward on why we use self, let’s quickly recap the difference of Instance Method & Class Method.
- Instance Method: We need to create an instance, and must be called on an object. WE MUST INSTANTIATE IT! Instance method provides functionality to an instance of a class.
- Class Method: Always starts with def self.class_method_name and ends with end. Class method provides functionality to a class itself.
Another time to use self is when we want to change the receiver.
class Ticket
def assign_number
1
end
def self.assign_number
2
end
end
Ticket.new.assign_number #=> 1
Ticket.assign_number #=> 2
Why do we use it?
By using self, it can help us distinguish between a local variable and a method and it makes the code reusable. Let’s look at the code below.
class Personattr_accessor :name def initialize(name)
@name = name
end def self.all
self
end def print_name #instance
puts self.name
end def capitalize_name
print_name = "Yi"
puts self.print_name
endend
If we were to change the class Person to something else, we don’t need to change self.all and reuse the same code. Since self will refer to the class, even by changing the class to Pets, we can keep our code.
If by any chance if you have a local variable (scope- inside of the method)name that is the same as an instance method(scope -anywhere inside of instance method) and a variable, You’d be able to distinguish a method and a variable.
One thing to always remember is that no matter which context the code finds itself. self always refers to one and only one object at any given time.