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.

def print_data(value)
puts value

Within a method, you can organize your code into subroutines invoked from other areas of their program.

Let’s see this other method.

def add_one(number)
number + 1

def add_two(number)
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:

class Name
# 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.

class Person
def initialize(name)
@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:

def self.from_the_class
"Hello, from a class method"

An instance method provides functionality to one instance of a class. For example:

def from_an_instance
"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 SayHello  
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.

Class Person
attr_accessor :name
@@all = []

def initialize(name)
@name = name
@@all << self

In this program, we did the following:

  1. Start by creating the Class Person.
  2. Follow by setting instance variable name as setter and getter (basically, read and write access) in order to access the variables outside the class.
  3. Initialize that name instance variable and also initialized the @@all class instance.
  4. 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!



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Norberto Santiago

Norberto Santiago

Norberto Santiago is a bilingual full-stack developer. Currently living in the Twin Cities.