The other type is called singleton method, which can only be called by one object, not the class of the object, nor other objects belonging to the same class as the object. The singleton method is defined as follows
#category1=Category.new
def category1.last_updated_at
#blahblah
end
After this definition, only the category1 object can call the last_updated_at method
Inside the definition of the Category class (outside the method), self refers to the Category itself, so
def Category.last_udpated_at
#blahblah
end
can be replaced by
def self.last_udpated_at
#blahblah
end
This is the method defined in your question. Do you understand now? In essence, it is the singleton method of Category, and Category is a class. The singleton method of a class is also called the class method of the class and can only be called by the class itself.
It should be noted that a class method is a singleton method, but it is a singleton method of a class. Other than that, there is nothing special about it. Just like the instance method is held by the class to which the object belongs, the singleton method is also held by something called the eigenclass of the object. There is a lot more knowledge about eigenclass, so I won’t mention it here. I will talk about it in the article.
self
指向当前 class,所以这种定义方法会定义出 class 方法(class method),如果不加self.
will define the instance method.This way of writing
in your exampleself.
is equivalent to:There are two types of methods that a ruby object can call.
One type is defined in the class of the object, called instance method, such as
Then the instance of Category can call this method, but the Category itself cannot
The other type is called singleton method, which can only be called by one object, not the class of the object, nor other objects belonging to the same class as the object. The singleton method is defined as follows
After this definition, only the category1 object can call the last_updated_at method
Ruby classes are also objects, and they can also define their own singleton methods, such as
In this way, only Category can call the last_updated_at method, but not its instances, nor other classes, nor any other objects to be exact
Inside the definition of the Category class (outside the method), self refers to the Category itself, so
can be replaced by
This is the method defined in your question. Do you understand now? In essence, it is the singleton method of Category, and Category is a class. The singleton method of a class is also called the class method of the class and can only be called by the class itself.
It should be noted that a class method is a singleton method, but it is a singleton method of a class. Other than that, there is nothing special about it. Just like the instance method is held by the class to which the object belongs, the singleton method is also held by something called the eigenclass of the object. There is a lot more knowledge about eigenclass, so I won’t mention it here. I will talk about it in the article.
Add self to define a class method, otherwise it is an instance method
Define class methods can also be