In Ruby, what is the difference between a class method and a class's singleton method? -
In Ruby, what is the difference between a class method and a class's singleton method? -
i've seen in code before , read in grounded rubyist david a. black, there no examples of utilize cases help me understand why want define singleton method on class this:
class vehicle class << self def all_makes # code here end end end
how above singleton method on class different usual class method this:
class vehicle def self.all_makes # code here end end
yehuda katz made first-class writeup of differences (among other things). can find here.
to give short summary. when defining class, self
keyword refers class itself. so, when self.method
defining new method on person class. every class has metaclass, known singleton class, can accessed , modified. in case of class << self
opening singleton class , modifying value. functionally, result same, class beingness modified different.
ruby singleton
Comments
Post a Comment