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,

ruby self class method - Shubham Mishra


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! 💻

Get to know answers for common search on Google : A blog for posts which can help you for daily life problems, such as where to get free images, Topic suggestion for the blog.

Computer Science algorithms and other knowledge share : A blog for posts such as best search algorithm, Top interview questions for diffrent technologies, knowledge share for some frameworks or programming languages for the interview or in general terms.

My ideas to solve real world problems : A blog where me shared and presented my ideas to solve a real world problems, this will be interesting for me.

Future of computer science technology discussed : A blog where me discussed about the future of computer science and new technologies which will change our way for looking to solve problems.

Ruby on Rails Web development Blog : As the name suggest, it is the blog for sharing few knowledge about RoR web development framework.

Comments

Popular posts from this blog

Rails Postgresql Query timeout set and skip during migration

Rails Sync Postgresql and Elasticsearch Database of a Model

Ruby on Rails configure CORS setting with rack cors gem