Method Overloading in ruby

muktesh Deshpande
2 min readOct 30, 2019

--

Ruby does not support method overloading. Yes, that’s right. If you are trying to implement method overloading in ruby that means you must have at least used one other language that supports it like Java and probably you thought, just like me, that every object-oriented language has method overloading. Well as the first line says it is not true. So let’s see why ruby does not support method overloading.

So what is method overloading? Method overloading is two or more methods having the same name but called with variant arguments. This difference in arguments may because of the variable number of arguments like shown in the first example or due to different types of arguments (example 2) or a mix of both (example 3)

example 1
example 2
example 3

Ruby is a dynamically typed language, i.e. there is no type declaration. So method overloading with different types of arguments can not be done in ruby.

In the second case, the last method defined will overwrite previous methods and calling any previous method will result in ArgumentError

This does not make ruby any less attractive though. Ruby supports default arguments and variable number arguments. Due to which you don’t miss method overloading in ruby.

--

--