Class Methods

What are they?

Class methods are methods that can only be called on a class. They are different from instance methods in that instance methods are called on an instance of a class. Compositionally they are very close as both are created inside a class. However class methods start with selfand then the method name. Take a look at the picture below.

Instance methods do not need the self when defining the method name. Take a look below.

Are they useful?

This is interesting, since some people do not particularly like class methods, they think of them as relics of the past. While others see class methods as a way to store application configurations, such as how many instances of a particular class have been created. Check out the RubyMonk resource for more information.


RubyMonk RailsTips