I'm looking at this code in a Ruby library.
Am I correct in assuming that self.class.delete
calls the class method called delete
on the current object - i.e. the object referenced by self
.
def delete!
self.class.delete(self.key)
end
I'm looking at this code in a Ruby library.
Am I correct in assuming that self.class.delete
calls the class method called delete
on the current object - i.e. the object referenced by self
.
def delete!
self.class.delete(self.key)
end
That's correct, disregarding the fact that ruby's 'class methods' are actually class instance methods.
Yes, it's a class method. As for whether it's calling it on the current object, it depends on how you're using the terminology. It's calling the method delete
of the current object's class with the argument self.key
.
It calls the class method delete
for the class of self.
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