Ruby Class Methods Tutorial

In this section, we will learn what the Class Methods are and how to use them in Ruby.

What is Class Method in Ruby?

A class method is a type of method that can be directly invoked using the name of that class!

Basically, in order to use such type of methods, you don’t need to create an instance (object) from the class first and then call the target method!

All you need is to use the name of the class and then put the name of the target method after it just like the way we call an instance method using an object.

To give you an example of class method, we can refer to the `new` method we’ve been using so far whenever wanted to create an object from a class!

The new method is a class method and so we use the name of the class to invoke it.

Be aware that class methods are only accessible using the name of the class itself! Basically, it’s not like that every object we create from a class will have its own copy of the target class method! There will be only one copy of the method and is only accessible using the class name.

One reason of why we use such type of method is because there are some general types of instructions that don’t need to create an object first in order to use them! Basically, these types of instructions (codes) are general enough that don’t define the behavior of a specific object!

For example, let’s say you want to take a random number every time you call a method! Well, such type of instructions don’t need to be set in an instance method to be used! You can simply put the code in a class method and every time you need it, just call the method with the name of the target class. Therefore, no need to create a new object and waste the memory space because of the unnecessary need for object creation.

Ruby Class Method Syntax:

There’s almost no difference between the way we create a class method and an instance method!

The only major difference is that we prefix the name of the method with the name of its owner class and put a dot in between.

For example:

def ClassName.method_name



The `ClassName` here is the name of the owner class that we defined the method in its body.

Also other than the name of the class as the prefix of the method, we can use the keyword self as well!

For example:

def self.method_name



This is still a class method.

Example: creating and calling a class method in Ruby

class Random
    def self.random_integer
        return rand(100)

puts Random.random_integer

puts Random.random_integer

puts Random.random_integer





How does class method work in Ruby?

In the example above, you can see the Random class has one Class Method with the name random_integer which produces random number every time we call the method.

As you can see, we didn’t create an instance method first in order to call this method!

Note: the `rand()` method you see in the body of the `random_integer` is a Ruby built-in method and produces a random number between 0 to the number we’ve set as the argument of the method.


Top Technologies