Acting as Individuals and as a Group

Instance Methods vs. Class Methods

Posted by 'Tine Zekis on May 17, 2015 · 5 mins read

Assignment: Technical Blog | Object-Oriented Programming

When we create a class in Ruby, we are able to create several instances of that class (objects). As we have discussed in a previous post, objects, or instances of a class, are the nouns of an Object-Oriented Programming (OOP) language. And if the objects are the nouns, then methods are the verbs. Methods, are the focus of this post: we will discuss the difference between instance methods and class methods.

As you may have guessed, instance methods get performed on an individual instance of the class, whereas class methods are performed on all instances of the class (the class as a whole). In order to illustrate this difference, let us consider the example of a Car class. Each car, an instance of the Car class, will have its own attributes, which are called instance variables. Let us say that our instances are distinguished by make, model, color, and year:

My first car was an emerald green '98 Honda Accord. I loved that car. So in honor of her, we will name our instance my_first_car. My current car is a black '99 Honda Civic. It is much less accomplished, but it deserves its own instance nonetheless, I suppose. So here are our two instances:

So now that we have a couple of cars to talk about, let's have them do things. First, let's create a couple of methods: one that will drive the car, and one that will tell us the mileage on each car. What kind of methods do you think we'll need for these tasks? Well, the mileage will not be the same for both cars, and we certainly won't be driving them in one go. So each instance of the Car class will need to perform these methods for itself. Thus, we will need instance methods:

In this case, calling the drive method on my_first_car will add 50 miles to the total @mileage and print
=> You drove 50 miles in your Honda Accord.
to the console. Calling the odometer method on my_current_car will return the @mileage that is currently on the Honda Civic and print
=> The Honda Civic has 0 miles on it.
to the console.

But what if we want a way to compare the mileage on both cars? This will be quite impossible with an instance method because one car does not know the mileage of another car. Here's where we need a class method. Let's start by introducing some class variables that will store the information we need: @@most_mileage, @@make_with_most, and @@model_with_most. Next, we will want to introduce the points during which we will need to check for which car is currently the one with the most mileage. That will look something like this:

Now, let's define what happens each time we check for which car currently has the most mileage, as well as what happens when we call the most_mileage class method:

Let's say that my_current_car has 75,000 miles on it. But my_first_car had 200,000 miles on it (and still ran like it was brand new!). Here's what it will look like for us to call the most_mileage method as well as the message that would be printed to the console:

Car.most_mileage
=> The Honda Accord has the most mileage with 200000 miles...and counting!
This method has successfully compared all existing instances of the Car class and told us which one has the most mileage.

So here are the major takeaways I hope you have gained from this post:

  1. Instance methods act on one particular instance of a class.
  2. Class methods act on all existing instances of the class at once
  3. My first car was glorious and had over 200,000 miles on it before it was sadly totaled.
'Tine out.

Resources: