# Life without Method Overloading?

**URL:** <https://www.ruby-forum.com/t/life-without-method-overloading/187171>\
**Category:** Ruby\
**Created:** [April 19, 2010, 7:52am UTC](https://www.ruby-forum.com/t/life-without-method-overloading/187171 "2010-04-19T07:52:56Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![derek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b38774/32.png) [@derek](https://www.ruby-forum.com/u/derek)\
**Post date:** [April 19, 2010, 7:52am UTC](https://www.ruby-forum.com/t/life-without-method-overloading/187171/1 "2010-04-19T07:52:56Z")

</div>

How do Ruby programmers handle method overloading? In Java, I could  
easily create several methods of the same name that accept a variety of  
input.

I know in Ruby, using \*args you can accept an unlimited number of  
parameters. Do I just this with a series of if statements?

Or is there a common way Ruby programmers handle this?

Thanks again,  
Derek

---

<div class="post-metadata">

**Author:** ![derek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b38774/32.png) [@derek](https://www.ruby-forum.com/u/derek)\
**Post date:** [April 19, 2010, 8:27am UTC](https://www.ruby-forum.com/t/life-without-method-overloading/187171/2 "2010-04-19T08:27:49Z")

</div>

Hi,

> Or is there a common way Ruby programmers handle this?

There are some common patterns, like what you find in Rails for example:

[http://railsbrain.com/api/rails-2.3.2/doc/index.html?a=M002313&name=find](http://railsbrain.com/api/rails-2.3.2/doc/index.html?a=M002313&name=find)

I think overloading is very much attached to static typing and not as  
useful in dynamic languages as it seems. Creating more methods is free  
and generally more expressive. Polymorphism is implicit (look up what we  
call ‘duck-typing’ here). I find that I rarely have the need for  
overloading in Ruby (or that I overload all the time, depending on which  
way you look at it - no type signatures means that you can pass in  
anything at all) - and when I need it, I use the rails-kind named  
arguments.

greetings,  
kaspar

---

<div class="post-metadata">

**Author:** ![derek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b38774/32.png) [@derek](https://www.ruby-forum.com/u/derek)\
**Post date:** [April 19, 2010, 8:49am UTC](https://www.ruby-forum.com/t/life-without-method-overloading/187171/3 "2010-04-19T08:49:28Z")

</div>

On Sun, Apr 18, 2010 at 10:53 PM, Derek C.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> How do Ruby programmers handle method overloading? In Java, I could  
> easily create several methods of the same name that accept a variety of  
> input.
> 
> I know in Ruby, using \*args you can accept an unlimited number of  
> parameters. Do I just this with a series of if statements?

Mostly, I think instead of creating several methods of the same name,  
Ruby  
programmers tend to create methods with different names. Some of the use  
cases of overloading can also be addressed with optional arguments,  
trailing  
hash arguments, and so on.

---

<div class="post-metadata">

**Author:** ![derek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b38774/32.png) [@derek](https://www.ruby-forum.com/u/derek)\
**Post date:** [April 19, 2010, 9:58am UTC](https://www.ruby-forum.com/t/life-without-method-overloading/187171/4 "2010-04-19T09:58:33Z")

</div>

On Mon, Apr 19, 2010 at 1:53 PM, Derek C.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> How do Ruby programmers handle method overloading?

ruby programmers do not need it

> In Java, I could easily create several methods of the same name that accept a variety of  
> input.

maybe java people need it 🙂

> I know in Ruby, using \*args you can accept an unlimited number of  
> parameters. Do I just this with a series of if statements?

no. you’re doing it like in static languages.

> Or is there a common way Ruby programmers handle this?

it comes natural when you are _object_-oriented. there will be no need  
for overload. you wont even think about it.

maybe you could show us a sample use case of method overloading since  
i cannot think of one right now 😉

best regards -botp

---

<div class="post-metadata">

**Author:** ![derek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b38774/32.png) [@derek](https://www.ruby-forum.com/u/derek)\
**Post date:** [April 19, 2010, 9:35am UTC](https://www.ruby-forum.com/t/life-without-method-overloading/187171/5 "2010-04-19T09:35:08Z")

</div>

On Mon, Apr 19, 2010 at 12:53 AM, Derek C.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)wrote:

> ## Derek
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

An easy thing to do is just normalize the data.

def join\_strings(a,b)  
a.to\_s + b.to\_s  
end

join\_strings ‘a’ , ‘b’ # =\> “ab”  
join\_strings 1 , 2 # =\> “12”

---

<div class="post-metadata">

**Author:** ![derek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b38774/32.png) [@derek](https://www.ruby-forum.com/u/derek)\
**Post date:** [April 19, 2010, 10:39am UTC](https://www.ruby-forum.com/t/life-without-method-overloading/187171/6 "2010-04-19T10:39:16Z")

</div>

> maybe you could show us a sample use case of method overloading since  
> i cannot think of one right now 😉

Sure. A simple example of what I wanted to do (using method overloading  
in this example for lack of better know-how):

Class XYZ

def initialize(title, days, time, professor)  
@title = title  
@days = days  
@time = time  
@professor = professor  
end

def initialize(title, days, time, professor, lab\_time, lab\_days)  
@title = title  
@days = days  
@time = time  
@professor = professor  
@lab\_time = lab\_time  
@lab\_days = lab\_days  
end

end

---

<div class="post-metadata">

**Author:** ![derek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b38774/32.png) [@derek](https://www.ruby-forum.com/u/derek)\
**Post date:** [April 19, 2010, 10:47am UTC](https://www.ruby-forum.com/t/life-without-method-overloading/187171/7 "2010-04-19T10:47:44Z")

</div>

On Mon, Apr 19, 2010 at 10:39 AM, Derek C.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > maybe you could show us a sample use case of method overloading since  
> > i cannot think of one right now 😉
> 
> Sure. A simple example of what I wanted to do (using method overloading  
> in this example for lack of better know-how):

This one is easy:

Class XYZ  
def initialize(title, days, time, professor, lab\_time=nil,  
lab\_days=nil)  
@title = title  
@days = days  
@time = time  
@professor = professor  
@lab\_time = lab\_time  
@lab\_days = lab\_days  
end  
end

Jesus.
