Instance Methods & Class Methods in Ruby
Published: 2023-06-30 21:36:16

Ruby is a dynamic object oriented programming language that was created in the mid-1990s by Yukihiro “Matz” Matsumoto. He was a master of many programming languages and combined the best parts of all of them to create Ruby. This resulted in a powerful and flexible programming language that is very popular among developers. One of the key fundamentals of Ruby is its object-oriented nature, which revolves around objects and classes.

In Ruby, classes are used to define what behavior and attributes are applied to objects. A class is best thought of as a data type and serves as template for creating objects, while objects are single instances of a class. With that being said, developers use ‘Instance Methods’ and ‘Class Methods’ to help define the desired behavior and attributes of an object or a class.

When writing both instance and class methods in Ruby, there are several important tips to keep in mind. First and foremost, it's crucial to choose meaningful names for your methods that accurately reflect their functionality and purpose. This ensures that the purpose of the method is clear and easily understood by other developers.

Similarly, when defining parameters for methods, it's important to choose descriptive names that act as placeholders for the actual values that will be passed into the method. This enhances the readability and maintainability of your code.

To make your methods easier to understand, it's best to simplify the method body. This can be achieved by breaking down the tasks performed by the method into smaller, hyper-specific functions. By doing this, the code becomes more modular which enhances code readability and maintainability.
Lastly, always remember to test your methods thoroughly to ensure they are working as intended.  Writing test methods or simply testing out method calls in the terminal can help catch any bugs or issues that may arise in your code. 

With all of that in mind, let's get into the specifics of instance methods and class methods, and how to use them.

## Instance Methods

Instance methods are defined within a class definition and they are used to define a specific behavior for an object. They are useful when you want different instances of the same class to have different behaviors.

**Defining Instance Methods:**

Instance methods are defined as follows:

![Instance Method Def](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y7tygk15k8it96ok63y3.png)

The method is defined with the class definition of Circle. The def keyword is used followed by the name of the method.  Then, the code that will define the behavior of the method will be written on the following line. The end keyword completes the method definition.

**Calling Instance Methods:**

To call an instance method on an object, we would need to create a new instance of the class and then use dot notation to call a specific method.

![Calling Instance Methods](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/aekulkf1777umjyftn0r.png)

In this example, we created an instance of the Circle class called circle1 and passed in the parameter "5" to set its radius upon initialization.  Then, we called the "area" instance method on circle1 which returned 78.5.

**Using 'self' Keyword Within Instance Methods:**

When using the 'self' keyword in an instance method, 'self' is referring to the object that the method is being called on.

!['self' Keyword](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0yzlo66vm5ggf4wp5b0h.png)

In this example, when we call print_area on circle1, the message is printed out to the console. By calling the area method on 'self', the code is more dynamic and can be used across many instances of circles that are created.

Instance methods are best used when you are trying to define a behavior specific to an individual object or instance of a class. 

## Class Methods

Class methods are used to define behavior for the entire class as a whole as opposed to a particular instance of that class.

**Defining Class Methods:**

To define a class method, you use the def keyword followed by the method name and the method body that gives the method its specified behavior.  The 'self' keyword is prepended to the method name to indicate the method belongs to the class itself and not an instance of that class.

![Define Class Method](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9l2hlfvlyhcr5qtk8at3.png)

**Calling Class Methods:**

To call a class method we simply invoke the method directly on the class by using the class name followed by the method name. 

![Calling Class Methods](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/84em7j3gx7uz2pho65b9.png)

In this example, we have a class of Calculator and a method called 'self.sum'. Remember, since the method is prepended with 'self', we know it is a class method. The method takes two parameters, num1 and num2, and prints the result.  When sum is called on Calculator with (5,5) passed as parameters, the result is 10.

Class methods are best used when you want to perform an action that is not specific to an individual instance, but rather the class as a whole.