Ruby Class Methods Vs Instance Methods
Published: 2020-01-07 01:56:27

When we first learned about classes and relationships, I struggled to grasp the difference between Class and Instance Methods. It was frustrating not having a clear understanding of these two methods. I constantly called instance methods on a class and wondered why I kept getting errors.

Class Method Vs Instance Method: In a short simple definition, class methods calls on the whole class, while instance methods calls on an instance of a class. 

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/cxlggsctynvj3k7zt1y4.png)

In this example, we are using the class to call on the class_method.    
![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/z39oyoa2muib3h5joa05.png)

However, we cannot use the class to call on an instance method directly.

###To call an instance method:

In the example below, an error is given when you call an instance method on a class. You cannot call an instance method on a class like you can with class methods.

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/cdcip04i88s8gh3yndvq.png)

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/apuwfgjsn98lnfxluo9v.png)

To call this this instance method. We would need to store an object in a variable, then call on the instance method with that variable, like so:

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/u11oq3dsmcc34fyhg3d0.png)

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/a8uk3nmnm62fy34nyb49.png)

###Song class example:
![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/0lf1v4rix953n9bmjonw.png)




In this example, @@all is indicated as a class method. Macros are set up for name, genre, and artists which are instance methods. 

Let’s make some songs!

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/24aw0tm4oopfc5cglihr.png)


I stored the new songs in a variable to make it more accessible when we need it. For now, we have a few songs stored in our Song class. What if I wanted to see all my songs? This would be a great way to utilize our class method! Since we need to view all objects stored in class, we would need to call on the whole class to return all songs that were stored in this class. Remember class methods call on the whole class!

So it would look something like this:

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/8629bginif7toevueb74.png)

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/4htpl6pr0uku31bz8qpk.png)

Now we have all our instances displayed (or all our songs).

What if I wanted just to get the name of one song from all our songs? We would have to use an instance method! In this case, we would use our name method to get the name of this first instance. But wait! This is an instance method. So we can’t use Song.name! Think about it. The class of Song has more than one song stored in it. How will it know which name I want to return? This is where we use our variables to call on our instance methods.

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/v2n90gxc9lu1j4okkd11.png)

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/cyt8stq3az4c5l3izjgs.png)

Now we have just the name of a single instance, using an instance method!