Ruby Self and its role
Hi, This is Shubham Mishra from India, this is the part of Ruby on Rails exploration journey. In this post we will discuss about the ruby's self keyword and its usage. You may have come up with self while working on the existing code base, but not got the significance of it, then read this post to understand it in a simpler manner. Even if you are not a ruby developer then also this article will be helpful since self is used in almost many programming languages.
Wait ruby's self is like above image only, it is representing the current object in the current context.
Confused? 🤪, wait let me tell you with the example,
Now lets decode above code to understand what is the self is,
Firstly, we have a class Student with a constructor to initialise the instance variable score, and a method named :show_info , Then we have created a instance of Student class, and stored the object into variable s1, Now lets decode specifically the :show_info method's content.
Now, when we called show_info on object s1 , it called the method and now we are inside the method, as we are on line puts self, Here, the self is inside the method which is instance method of class Student, and being called from Object s1, which means it is (method) inside the context of object s1.
So, once the object s1, is created at the same time, the method show_info, is being instantiated and whenever the same method is called by the object, it (method) will be in the context of that object, hence calling self will give the Object only.
You can even observe how printing self.class gives the 'Student', as self is the object s1 at that time.
Ok, so now other than this basic role of self, let's have a look in other angles of self's usage.
Opps!, a little confusing do not worry, lets understand above class code
In the class Student we have initalized a variable score in the intializer method which is the constructor in programming language. Now the class_room method, it will return the string '7th class' whenever it being called by any instance (object) of class Student.
Now lets focus on the self.school method, why we have written self. before the method name. Well, in OOPS (Object Oriented Programming language) we can have few properties which are common for all the objects created for that class, it is class property. Since it is common property so it can be accessible by all the instances (objects) created of that class. Lets see below diagram for better understanding,
As, it can be seen the definition school can be accessible by all the instances since it is the class property. (Rectification in above image, all the instances will also have def show_info method other than def class also)
Understood the purpose of self before the method's definition. Now in the show_info when we called self.class, it is giving us the Student and then when we called school on this returned value, it printed out the content of school method.
Now one more point to note is that inside definition show_info, we have one variable class, and it printed class variables content when it is called, but when we called self.class, in the next line, it printed the content of the class method, as we already know that self here is the s1 Instance of Student class.
Now I think enough reading, I would suggest you to open your rails terminal and play with the code! 💻
Comments
Post a Comment