Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does self.class.delete call a class method?

Tags:

ruby

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
like image 626
franz Avatar asked Jan 23 '23 13:01

franz


1 Answers

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
like image 153
toholio Avatar answered Feb 01 '23 22:02

toholio