Introduction
The high-level, interpreted programming language Ruby supports a number of different programming paradigms. It was created with a focus on productivity and simplicity in the programming. And this article will discuss such a simple topic in ruby, i.e. Overloading methods in Ruby. Overloading is an interesting and valuable player in Object-Oriented Programming's world. So let's directly jump into the topic.
Overloading Methods in Ruby
Method overloading enables a class to have multiple methods with the same name but various method declarations, such as different types of parameters, different types of arguments, and different order of arguments. And in Ruby, method overloading may be accomplished by specifying two methods with the same name but different signatures.
These signatures can be
- Arguments using several data types, for as method(int a, int b) or method (String a, String b)
- Multiple arguments, such as method(a) or method(a,b).
The first method does not allow for method overloading since Ruby does not have a data type definition (dynamically typed language). The only way to define the given technique is def (a,b).
So, There can only be one named method for each Ruby class. However, you may implement logic within that same method that differs based on the quantity and kind of parameters handed in.
By changing the method's parameter count, overloading may be implemented in Ruby in one possible way. Although overloading methods with various sorts of parameters is possible, it becomes extremely challenging to maintain a record of the code. Below is an example of each strategy.
Method overloading by varying the number of arguments
Code
class Test
def self.display(*args)
case args.size
when 1
puts "1: Hello #{args[0]}"
when 2
puts "2: I am #{args[0]} #{args[1]}"
when 3
puts "3: Today #{args[0]} #{args[1]} #{args[2]} in Ruby language."
end
end
end
puts "Overloading using Class Method"
Test.display"Ninjas"
Test.display"Ankit!", "Hope you doing great."
Test.display"We'll", "learn", "Overloading Methods"
Output
Overloading using Class Method
1: Hello Ninjas
2: I am Ankit! Hope you doing great.
3: Today We'll learn Overloading Methods in Ruby language.
A class method is the display() one. A class method in Ruby gives the class itself functionality. Therefore, we immediately call the method by utilising the class name, the dot operator, and the method name. Class methods are by default public.
Using instance methods
Code
class Test
def display(*args)
case args.size
when 1
puts "1: Hello #{args[0]}"
when 2
puts "2: I am #{args[0]} #{args[1]}"
when 3
puts "3: Today #{args[0]} #{args[1]} #{args[2]} in Ruby language."
end
end
end
ob1 = Test.new
ob2 = Test.new
ob3 = Test.new
puts "Overloading using Instance Method"
ob1.display"Ninjas!"
ob2.display"Ankit!", "Hope you doing great."
ob3.display"We'll", "learn", "Overloading Methods"
Output
Overloading using Class Method
1: Hello Ninjas
2: I am Ankit! Hope you doing great.
3: Today We'll learn Overloading Methods in Ruby language.
Class instances are given functionality via the instance method display(). To call the class' instance method, we must first create instances of the class. The class name cannot be used to directly call an instance method.
Explanation
Strongly typed languages like C++ and Java need the creation of several variations of a single method with various parameters. For instance, the StringBuffer class in Java provides over ten different iterations of the add function, including versions that accept strings, Booleans, and more.
StringIO is Ruby's version of StringBuffer, while StringIO# is Ruby's version of the add function. That method in Ruby can accept any type of object, but it can only be defined once. For accepting multiple types of objects, it is not necessary to write different versions of the method. If type checking is required, such as verifying if an object has a string representation, it should be added to the method body rather than the method description.
Ruby's loose typing eliminates most of the method overloading requirement. Its default arguments, variable-length argument lists, and keyword arguments eliminate the majority of the remaining instances.