# How does overload work in ruby?

**URL:** <https://www.ruby-forum.com/t/how-does-overload-work-in-ruby/117893>\
**Category:** Ruby\
**Created:** [October 22, 2007, 7:18am UTC](https://www.ruby-forum.com/t/how-does-overload-work-in-ruby/117893 "2007-10-22T07:18:00Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![shuaib85](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bbce88/32.png) [@shuaib85](https://www.ruby-forum.com/u/shuaib85)\
**Post date:** [October 22, 2007, 7:18am UTC](https://www.ruby-forum.com/t/how-does-overload-work-in-ruby/117893/1 "2007-10-22T07:18:00Z")

</div>

Hello guys

I was trying to overload one method but it does not work like Java.  
I noticed it only uses the first method that it meets.

is there any special way for overloading in ruby.

class Overloading

def sum(a, b)  
return a + b  
end

def sum(a, b, c)  
return a + b + c  
end  
end

obj = Overloading.new  
puts obj.sum(5, 7)  
puts obj.sum(5, 6, 7)

the output  
overloading.rb:13:in `sum’: wrong number of arguments (2 for 3)  
(ArgumentError)  
from overloading.rb:13

any idea?

Regards  
Shuaib

---

<div class="post-metadata">

**Author:** ![shuaib85](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bbce88/32.png) [@shuaib85](https://www.ruby-forum.com/u/shuaib85)\
**Post date:** [October 22, 2007, 7:27am UTC](https://www.ruby-forum.com/t/how-does-overload-work-in-ruby/117893/2 "2007-10-22T07:27:03Z")

</div>

On Oct 22, 2007, at 7:17 AM, Shuaib Z. wrote:

> return a + b  
> end
> 
> def sum(a, b, c)  
> return a + b + c  
> end

Ruby does not support method overloading that way. As you founded out  
only one of the definitions of sum survives. However, you can define  
defaults for optional parameters like this:

def sum(a, b, c=0)  
a + b + c  
end

– fxn

---

<div class="post-metadata">

**Author:** ![shuaib85](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bbce88/32.png) [@shuaib85](https://www.ruby-forum.com/u/shuaib85)\
**Post date:** [October 22, 2007, 7:28am UTC](https://www.ruby-forum.com/t/how-does-overload-work-in-ruby/117893/3 "2007-10-22T07:28:13Z")

</div>

On 10/22/07, Shuaib Z. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I was trying to overload one method but it does not work like Java.  
> I noticed it only uses the first method that it meets.

There is no overloading in Ruby, only overriding, and the last  
definition overrides any previous definition.

The case for what you’re wanting is actually not that hard:

class Summation  
def sum(\*args)  
args.inject { |a, i| a + i }  
end  
end

As you can see, for this particular sort of behaviour, you don’t even  
need a method. \*args, as well, allows for “infinite” arguments.  
Specific arguments can be reached with:

class Summation  
def sum(first, \*rest)  
rest.inject(first) { |a, i| a + i }  
end  
end

-austin

---

<div class="post-metadata">

**Author:** ![shuaib85](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bbce88/32.png) [@shuaib85](https://www.ruby-forum.com/u/shuaib85)\
**Post date:** [October 22, 2007, 10:19am UTC](https://www.ruby-forum.com/t/how-does-overload-work-in-ruby/117893/4 "2007-10-22T10:19:34Z")

</div>

Hi,

Am Montag, 22. Okt 2007, 14:25:49 +0900 schrieb Xavier N.:

> > return a + b + c  
> > end
> 
> Ruby does not support method overloading that way. […]  
> However, you can define defaults  
> for optional parameters like this:
> 
> def sum(a, b, c=0)  
> a + b + c  
> end

If you want to allow the caller to select the default value  
explicitly you may say

def sum a, b, c = nil  
c ||= 0  
a + b + c  
end

Some methods even change their behaviour with the number of  
parameters they are called with, for example File#basename.  
You still may check whether the last parameter is nil. If  
you need to be 100% sure, you could write a C function;  
rb\_scan\_args() returns the number of arguments given.

Bertram

---

<div class="post-metadata">

**Author:** ![shuaib85](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bbce88/32.png) [@shuaib85](https://www.ruby-forum.com/u/shuaib85)\
**Post date:** [October 22, 2007, 2:31pm UTC](https://www.ruby-forum.com/t/how-does-overload-work-in-ruby/117893/5 "2007-10-22T14:31:58Z")

</div>

On 10/22/07, Bertram S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Am Montag, 22. Okt 2007, 14:25:49 +0900 schrieb Xavier N.:

> end  
> It seems to me that this is just a slightly less efficient way of  
> doing the same thing as the code you quoted.

In either case the caller isn’t specifying the default value, it’s  
overriding the default.

In the call sum(1,2) ruby will evaluate the default expression for c,  
in the call sum(1,2,3) it won’t since c has a value given by the third  
parameter. Making the default value of c nil, and then doing c ||= 0  
justs slows down both cases.

–  
Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)

---

<div class="post-metadata">

**Author:** ![shuaib85](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bbce88/32.png) [@shuaib85](https://www.ruby-forum.com/u/shuaib85)\
**Post date:** [October 23, 2007, 4:24pm UTC](https://www.ruby-forum.com/t/how-does-overload-work-in-ruby/117893/6 "2007-10-23T16:24:04Z")

</div>

On Mon, 2007-10-22 at 21:31 +0900, Rick DeNatale wrote:

> In either case the caller isn’t specifying the default value, it’s  
> overriding the default.
> 
> In the call sum(1,2) ruby will evaluate the default expression for c,  
> in the call sum(1,2,3) it won’t since c has a value given by the third  
> parameter. Making the default value of c nil, and then doing c ||= 0  
> justs slows down both cases.

It also means that if the caller really wants to pass nil, it’ll get  
turned to the default value.

- Arlen
