Static vs Class Methods in Ruby

Firmansah
5 min readMar 24, 2023

--

In Ruby, we can create both static methods and class methods, but what is the difference between them?

In this article, we will explore the differences between static methods and class methods in Ruby and when to use each one.

Introduction

Ruby is a dynamic, object-oriented programming language that is often used for building web applications. It provides developers with a great deal of flexibility and freedom, allowing them to create code that is both concise and expressive.

One of the key features of Ruby is its support for both static methods and class methods. These two types of methods are similar in many ways, but they have some important differences that make them useful in different situations.

Static Methods

Static methods are methods that can be called on a class without the need to create an instance of that class. They are also known as class-level methods. Static methods are useful when you need to perform an action that does not require an instance of the class. You can learn more about static method in this article

To define a static method in Ruby, we use the self keyword followed by the method name:

class MyClass
def self.my_static_method
# code here
end
end

We can then call this static method on the class itself, without the need to create an instance:

MyClass.my_static_method

Static methods are particularly useful when you need to perform a calculation or check a condition that is not related to a specific instance of the class. For example, if you need to calculate the square root of a number, you can create a static method that takes the number as a parameter and returns the square root.

Another use case for static methods is when you need to create a utility function that can be used across multiple classes. For example, if you need to convert a string to a date, you can create a static method that can be used across multiple classes.

Class Methods

Class methods are methods that can be called on a class and are intended to be used to create new instances of that class. They are also known as instance-level methods.

To define a class method in Ruby, we use the initialize method:

class MyClass
def initialize
# code here
end
end

We can then create a new instance of the class using the new method:

my_instance = MyClass.new

We can then call instance methods on this instance:

my_instance.my_instance_method

Class methods are particularly useful when you need to create new instances of a class. For example, if you have a class that represents a user, you can create a class method that creates a new user instance:

class User
def self.create_new_user
new_user = User.new
# code to set up new user
return new_user
end
end

You can then call this class method to create a new user instance:

new_user = User.create_new_user

When to Use Static Methods

Static methods are useful when you need to perform an action that does not require an instance of the class. For example, if you need to perform a calculation or check a condition that is not related to a specific instance of the class, you can use a static method.

Another use case for static methods is when you need to create a utility function that can be used across multiple classes. For example, if you need to convert a string to a date, you can create a static method that can be used across multiple classes.

When to Use Class Methods

Class methods are useful when you need to create new instances of a class. For example, if you have a class that represents a user, you can create a class method that creates a new user instance:

class User
def self.create_new_user
new_user = User.new
# code to set up new user
return new_user
end
end

You can then call this class method to create a new user instance:

new_user = User.create_new_user

Class methods are also useful when you need to define methods that are related to a specific class, but not to any instance of that class. For example, you might create a class method that returns a list of all users, or a method that calculates the average age of all users.

Performance Comparison between Static and Class Methods in Ruby

Static methods and class methods both have their own unique use cases, but how do they compare in terms of performance? We can use the benchmark method in Ruby to compare the performance of static and class methods.

We’ll start by creating a simple class with both a static method and a class method:

class MyClass
def self.class_method
1000000.times do
# do something
end
end

def static_method
1000000.times do
# do something
end
end
end

In this example, both methods perform the same operation 1,000,000 times. Now, let’s use the benchmark method to compare the performance of these two methods:

require 'benchmark'

my_class = MyClass.new

Benchmark.bm do |x|
x.report("class method") { MyClass.class_method }
x.report("static method") { my_class.static_method }
end

This code will run both the class method and the static method and output the results. Here’s an example of what the output might look like:

user     system      total        real
class method 0.010000 0.000000 0.010000 ( 0.014570)
static method 0.010000 0.000000 0.010000 ( 0.015942)

In this example, the difference in performance between the two methods is negligible. However, in more complex scenarios, one method may be significantly faster than the other.

It’s important to note that the performance of static and class methods can vary depending on the specific use case. In general, static methods are faster than class methods because they don’t need to create an instance of the class. However, if the method needs to access instance variables or methods, a class method may be faster.

In this section, we compared the performance of static and class methods using the benchmark method in Ruby. While the performance difference between the two methods in this example was negligible, it's important to test the performance of your methods in your specific use case to determine which method is faster.

Conclusion

Static methods and class methods are both useful in different situations. Static methods are useful when you need to perform an action that does not require an instance of the class, while class methods are useful when you need to create new instances of a class or define methods that are related to a specific class.

By understanding the differences between static methods and class methods, you can choose the right one for your situation and write more efficient and effective code in Ruby.

References

--

--