tags:

views:

118

answers:

3

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
+1  A: 

That's correct, disregarding the fact that ruby's 'class methods' are actually class instance methods.

cloudhead
A: 

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.

Chuck
+5  A: 

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
toholio
So self.class.delete is the same as Example.delete. Thanks.
franz