Singleton method vs. class method

后端 未结 5 935
一向
一向 2021-02-08 16:31

Are class method and singleton method doing the same or different? Here is an example.

class C
  def self.classmethod
    puts \"class method #{self}\"
  end
end         


        
5条回答
  •  梦谈多话
    2021-02-08 16:36

    In ruby class is an object as well. So, in your example classmethod is a singleton method for object C and singletonmethod is singleton method for object c.

    I highly recommend book "Metaprogramming Ruby" by Paolo Perrotta.

提交回复
热议问题