What's the difference between a class method and an instance method? Is there a related chapter?
A class method belongs to the class.
class Aclass @@someClassVariable = "I belong to the class" def self.getClassVariable() @@someClassVariable end end aclass = Aclass.getClassVariable() # => "I belong to the class"
The unit on classes is not very exhaustive or indepth, but we do get exposed to the fundamental concepts. More study will be in order to strengthen one's own skills and knowledge base.
I found this post on SO that very briefly demonstrates the basic difference:
class Aclass @@someClassVariable = "I belong to the class" def self.getClassVariable @@someClassVariable end def getClassVar self.class.getClassVariable end end puts Aclass.getClassVariable() # I belong to the class myClass = Aclass.new puts myClass.getClassVar() # I belong to the class
This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.