Class Methods and Instance Methods: What’s the Difference?
Today, I want to write something important to Ruby beginners, the difference between class and instance methods. As beginners, we can be overwhelmed by so much terminology. “What is a method?” “What does “iterating” data mean?” “What’s an argument?” So let’s take a deep breath and start step by step.
First, let’s start by defining what’s a method. We can define a Ruby method as a function. This function will return a value.
Within a method, you can organize your code into subroutines invoked from other areas of their program.
Let’s see this other method.
number + 1
number = add_one(number)
I’m calling method add_one(number) on method add_two when declaring the number variable. That’s the magic of programming. We’ll be connecting those methods. It’s as simple as done in the above example.
We declare those methods in a Class (first-class objects — each an instance of class Class). It is where we create the objects.
The syntax is as simple as this little example right here:
# some code describing the class behavior
So what’s an instance? Suppose I got a class and I created a method; how can I get an instance variable? Let’s take a look at the following code.
@name = name
We got the Class Person. Then we got the Method Initialize(name). Important: Initializers are an essential part of your Ruby programs. The body of the initialize method now does nothing else but assign the value of the local variable name to an instance variable @name. Basically, “name” is a local variable (exist within the definition of a module, method, class) to an instance variable and is going to be confined to the object it will be referred to. Now, we’ll be calling the instance variable name with an @. Like this, @name.
Now that we know what’s a Class, a Method variable, and an Instance variable. Can we understand what’s a Class Method and what’s an Instance Method?
Let’s check what’s a Class Method. A class method provides functionality to that class. For example:
"Hello, from a class method"
An instance method provides functionality to one instance of a class. For example:
"Hello, from an instance method"
See the keyword that makes the difference? The word self is signifying that it is a Class Method. We can also write Class methods like this:
class << self def from_the_class
puts "Hello, from a class method"
Another important detail about class methods. A class variable with @@. One thing we’ll do a lot when programming Ruby is the following.
@@all = 
@name = name
@@all << self
In this program, we did the following:
- Start by creating the Class Person.
- Follow by setting instance variable name as setter and getter (basically, read and write access) in order to access the variables outside the class.
- Initialize that name instance variable and also initialized the @@all class instance.
- Assign it an empty array in which will store every instance of the class.
Once we figure out each of both terms, it makes sense. It’s like getting all the pieces of a complicated jigsaw puzzle. And like every jigsaw puzzle, we got to start with the borders. In this case, this is the border. Understanding what it is and how it works will help us create good Object-Oriented programs. Now that we understood Ruby a little bit. Let’s keep having fun as we dive deeper!