Does self.class.delete call a class method?

I am viewing this code in the Ruby library.

Do I correctly assume that self.class.delete calls a class method called delete for the current object - i.e. The object referenced by self .

 def delete! self.class.delete(self.key) end 
+4
source share
3 answers

It calls the method of the delete class for the self class.

 class Example def self.delete puts "Class method. 'self' is a " + self.class.to_s end def delete! puts "Instance method. 'self' is a " + self.class.to_s self.class.delete end end Example.new.delete! 

Outputs:

  Instance method.  'self' is a Example
 Class method.  'self' is a Class 
+8
source

This is correct, ignoring the fact that ruby ​​"class methods" are actually instance class methods.

+1
source

Yes, this is a class method. As for whether it refers to the current object, it depends on how you use the terminology. It calls the delete method of the current object class with the self.key argument.

0
source

Source: https://habr.com/ru/post/1285988/


All Articles