Ruby is a popular dynamic, object-oriented programming language that is widely used for web development. While working with Ruby, creating static methods in a class using the self
keyword is one of the unique features. Using self
in a class allows us to create methods that can be called on the class itself, rather than on an instance of the class. This means that we don't need to instantiate or create a new instance of a class for calling the methods inside the class.
What is self in Ruby?
In Ruby, self
is a reserved keyword that always refers to the current object. It is important to note that in Ruby, classes are also objects, but the object self
refers to frequently changes based on the situation or context. If you're in an instance, self
refers to the instance. If you're in a class, self
refers to that class.
The Purpose of self
The purpose of self
inside class is that we don’t need to instantiate or create a new instance of a class for calling the methods inside the class. We just need to call the class followed by the method name, or it means the method is static. By using self
to create static methods, we can improve the performance of our Ruby on Rails application.
In addition to making our code more efficient, using static methods can also make our code more readable and easier to maintain. By using static methods, we can group related methods together and make it easier to understand the relationship between them.
Creating Static Methods with self
To create a static method in Ruby, we use the self
keyword followed by the method name. Here is an example of a class Person
with a static method name
using the self
keyword:
class Person
def self.name
puts "My name is Firman"
end
end
Person.name
In the above example, we define the Person
class and create a static method called name
using self
. We then call the name
method on the Person
class without needing to instantiate the class. The output of the code will be:
My name is Firman
Alternatively, we can wrap all of our methods with class << self
, which allows us to shorten the code writing:
class Person
def class_method
puts "This is class method"
end
class << self
def name
puts "My name is Firman"
end
def age
puts 24
end
end
end
Person.name
Person.age
Person.class_method # it will occur error
This code will output:
My name is Firman
24
By using class << self
, we can access static methods without needing to create an instance of the class. This can make our code more organized and efficient.
If we try to call non static methods Person.class_method
(class methods, note: we will discuss both in the next article) without instantiating the class it will occur error as shown below:
My name is Firman
24
Traceback (most recent call last):
test.rb:19:in `<main>': undefined method `class_method' for Person:Class (NoMethodError)
Did you mean? alias_method
Conclusion
Using the self
keyword in a class allows us to create and call static methods without the need to instantiate the class. This can help to improve the performance of our Ruby on Rails application. By using static methods, we can use the class itself as a container for related methods, rather than creating instances of the class. This can make our code more organized and efficient.
In summary, the use of static methods with the self
keyword in Ruby can improve code efficiency, readability, and maintenance. By using these techniques, we can create more efficient and organized code that is easier to maintain over time.