Class methods

Learn what class methods are and how to use them in Ruby.

What are class methods

We are already familiar with instance methods. However, there is another type of method called class methods. Some other programming languages refer to them as static methods or self methods. The same thing is true for variables: there are instance and class variables, though class variables are used less often. There are also local variables, which are only available inside methods. We need to understand the difference between instance and class methods (and variables, of course).

Imagine a technical drawing of an object, or see an example in the “Classes and Objects” chapter. This drawing has all sizes specified. The drawing, or class, can be used to produce an actual object, or instance. Sizes from the drawing make sense only when an object has been produced. When there is no object, sizes are just numbers on paper. The technical drawing says, “When an actual object (instance, in this case) is going to be produced, the size should be these values.” That’s why we call them instance variables.

However, there is also meta-information in the drawing, like who the author is. Imagine we’ve produced one thousand objects, or instances, and it turned out that the author was not Sam Smith, but Pat King. We would probably get an eraser and replace the wrong name with the right one.

For 1,000 already produced objects, nothing will change since it’s meta-information. It’s not the size of an object. It can exist without this information. However, if somebody will ask who is the actual author of this wonderful item, the answer will be different.

So, this meta-information is like class methods, or class variables. Sizes and the actual data that defines the behavior of a real object are instance methods, or instance variables.

Why some programmers don’t prefer class methods

Many programmers don’t prefer class methods and variables, and there is a reason for that. It’s not fun when we make a mistake and, for example, write the wrong author name in a technical drawing. However, we can fix it. We’ll iterate over each drawing and replace the name with the right one. But that’s half the trouble. What if the material was specified incorrectly? It should be steel, but was specified as plastic. When existing objects try to “get” what they are made of, they will get incorrect answers!

We often see it in the real world. In 2019, author of this course got a letter from a Honda car manufacturer saying:

“Statement from American Honda Motor Co., Inc. Re: Confirmed Rupture of Defective Takata Airbag Inflator… On March 29, 2019, following a joint inspection, American Honda and the National Highway Traffic Safety Administration (NHTSA) confirmed that a defective Takata driver’s airbag inflator ruptured in the crash of a 2002 Honda Civic on June 8, 2018, in Buckeye, AZ. … The vehicle involved in this crash had been under a recall since December 2014 to replace the Takata driver’s frontal airbag inflator…”

This is a serious issue. The objects—cars—have already been produced. But it turned out that some instance variable—the airbag—was specified incorrectly. There was a need for a recall, and Honda had to provide instructions on how to replace the airbag and spend millions of dollars to fix the issue for already produced objects.

So, we have to be careful with class—or static—methods and variables. There is no issue with instance variables; the amount of gas in the gas tank is a good example of such a variable. And if gasoline is low, we can just go to the gas station and refill our tank. Class methods and variables, once specified, shouldn’t change their behavior over time. We should be extra careful when combining both class and instance variables or methods. Let’s look at the code below with a class method:

Code example

Let’s look at this code below with a class method:

Get hands-on with 1200+ tech skills courses.