Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Overloading Methods in Ruby
2.1.
Method overloading by varying the number of arguments
2.1.1.
Code
2.1.2.
Output
2.2.
Using instance methods
2.2.1.
Code
2.2.2.
Output
2.2.3.
Explanation
3.
Frequently Asked Questions
3.1.
What makes the Ruby language special?
3.2.
List two advantages of Ruby.
3.3.
What is method overloading?
3.4.
Is Ruby a dynamically typed language?
3.5.
Difference between Overloading and Overriding.
4.
Conclusion
Last Updated: Mar 27, 2024
Easy

Overloading methods in Ruby

Author Ankit Kumar
1 upvote

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.

Introduction image

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"
You can also try this code with Online Ruby Compiler
Run Code


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"
You can also try this code with Online Ruby Compiler
Run Code


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.

Frequently Asked Questions

What makes the Ruby language special?

Ruby is a programming language that can be used for standard programming jobs, developing prototypes, and running servers. As a fully integrated object-oriented language, Ruby scales well.

List two advantages of Ruby.

The two advantages are that the language is only focused on objects. And The simple syntax makes it simple to learn.

What is method overloading?

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.

Is Ruby a dynamically typed language?

There is no data type declaration in Ruby, so Ruby is a dynamically typed language.

Difference between Overloading and Overriding.

If a method is being overloaded, the parameter has to be changed. If a method is overridden, the argument must be the same.

Conclusion

In this article, we've extensively discussed the overloading methods in Ruby. We discussed the overloading methods in Ruby along with an explanatory example. We have implemented the code for the given examples. 

When you have finished reading Overloading Methods In Ruby? Are you not eager to study and research more about Ruby? Chill. Coding Ninjas made your task easy. To learn, see Ranges in RubyConstants in RubyArrays in Ruby, or How to invoke global functions in Ruby?

You can check the official documentation of Ruby too.

Happy Coding!

Thankyou Image
Live masterclass