Not so long ago, taking up the study of ruby, I was confronted with the fact that I did not understand what class methods were, what was the difference between them and what they were for. In the textbook, which I am studying at the moment, this topic was not described in sufficient detail or I did not finish reading the detailed description, but in any case, it became interesting for me to figure it out and I found it useful to search for answers in google. This post is all that I managed to find and understand. Of course, for experienced ruby developers there is not much interesting here, but I hope that I can help the newcomers in the same language as me. If I intrigued you - I ask for cat.
First, let's define: in ruby all objects. It needs to be absorbed, learned and always remembered. Even classes are objects whose name is a constant. And with each object you can perform any actions thanks to the methods. Therefore, a reasonable question arises: is it possible to do something with classes through methods?
I think that many will guess that the answer to this question will be positive, I will even say more: in ruby each method is either an instance method or a class method, respectively. They differ, except for the method of creation, only in that the first methods are invoked with an instance of the class, and the second with the class. It is important to understand the difference between these two methods and to be able to determine when and which method to use. Therefore, we will consider class methods (class methood), but first let me remind you, or maybe for whom, and tell you what instance methods are.
Instance methods (instance metods):
Often used, usually with such methods, language learning begins in almost all textbooks. They are usually created in three ways:
')
Nothing complicated and everything is clear. They are used everywhere. For example: in rails, instance modules may be responsible for creating and deleting the same posts, well, or a frequently encountered method is the addition method.
Class methods (class methods):
Ruby is a very flexible and concise language, so it provides us with as many as four different ways to create class methods:
Actually the creation of these methods is also not difficult. All the presented methods work the same way and the only thing that distinguishes them is the taste of the developer. In the first and second methods, the same construction is used, with the only difference in the
self
variable. The third case is interesting because it creates an anonymous class, and the last one is very similar to creating a singleton. When I see code similar to
self.name_class_method
, I understand that this is a class method, so I agree with the opinion of people who say that in the first case, the code is more readable, as it creates less confusion.
When should you use these methods? Usually they are used when you are not dealing with individual instances of a class. The most commonplace example is rails validation methods. The code looks like this:
module ActiveRecord class Base def self.validates_of(...)
All the methods of the
arrt_*
family can also be referred to this example. Also, there is a wonderful
new
method that contains all instances of the
Class
class.
The interaction of instance and class methods with each other.
There are cases when it is necessary to call an instance method in a class method, ruby for this purpose provides an elegant way:
And sometimes there are times when, on the contrary, you need to call a class method in an instance method. For these purposes, you need to use the
class
method:
It is important to understand that
self
returns the value of a particular class, so sometimes there is confusion, when necessary, so that the heirs of this class return the parent method. For these purposes, you need to use instead of
self
directly the name of the parent class:
class Test1 def self.class_method "foo" end def make1 self.class.class_method end def make2 Test1.class_method end end class Test2 < Test1 def self.class_method "abc" end end b = Test2.new b.make1
PS: Thanks to comrade
walkman7 for helping to find a mistake in the last example.