# Over Loading

**URL:** <https://www.ruby-forum.com/t/over-loading/133055>\
**Category:** Ruby\
**Created:** [March 27, 2008, 7:18am UTC](https://www.ruby-forum.com/t/over-loading/133055 "2008-03-27T07:18:31Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![selva](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/13edae/32.png) [@selva](https://www.ruby-forum.com/u/selva)\
**Post date:** [March 27, 2008, 7:18am UTC](https://www.ruby-forum.com/t/over-loading/133055/1 "2008-03-27T07:18:31Z")

</div>

Hai All,

Question looks like simple Why ruby is not supporting over loading??

I think we can pass any number of argument as an array to method. is  
this the reason or any other ???

---

<div class="post-metadata">

**Author:** ![selva](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/13edae/32.png) [@selva](https://www.ruby-forum.com/u/selva)\
**Post date:** [March 27, 2008, 10:35am UTC](https://www.ruby-forum.com/t/over-loading/133055/2 "2008-03-27T10:35:47Z")

</div>

Because of Ruby type system

You cant write

def test(int i)

end

def test(string s)

end

In Ruby this is  
def test(arg)  
end

So you can do something like this  
def test(arg)  
if arg.is\_a?(String)  
else  
end  
end

---

<div class="post-metadata">

**Author:** ![selva](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/13edae/32.png) [@selva](https://www.ruby-forum.com/u/selva)\
**Post date:** [March 27, 2008, 1:10pm UTC](https://www.ruby-forum.com/t/over-loading/133055/3 "2008-03-27T13:10:21Z")

</div>

2008/3/27, Selvaraj S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Question looks like simple Why ruby is not supporting over loading??

You would have to ask Matz for details. Here are some potential  
reasons:

- Ruby is dynamically typed which makes overloading based on argument  
types impossible; you could only overload on argument list structure  
(mostly number of args)

- argument list structure based overloading is seldom useful and in  
those cases the implementer can do it himself - no need to burden the  
language with this.

- because of Ruby’s dynamic nature methods that are overloaded based  
on argument type in other languages often fall into one and need not  
be overloaded.

> I think we can pass any number of argument as an array to method. is  
> this the reason or any other ???

This might be part of the reason (see above).

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![selva](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/13edae/32.png) [@selva](https://www.ruby-forum.com/u/selva)\
**Post date:** [March 27, 2008, 1:31pm UTC](https://www.ruby-forum.com/t/over-loading/133055/4 "2008-03-27T13:31:54Z")

</div>

-----BEGIN PGP SIGNED MESSAGE-----  
Hash: SHA1

On Mar 27, 2008, at 10:34 AM, Ð˜Ð²Ð°Ð½ Ð•Ð²Ñ‚ÑƒÑ…Ð¾Ð²Ð¸Ñ‡ wrote:

> So you can do something like this  
> def test(arg)  
> if arg.is\_a?(String)  
> else  
> end  
> end

There are some projects out there that allow pattern matching on  
arguments to some extend. (search for “match” on rubyforge)

Basically, they work by defining a proxy method that does the pattern  
matching and then calling some predefined procs. Essentially,  
they define this test-Method dynamically.  
This is not overloading in a strict sense, but it comes close.

Greetings  
Florian G.  
-----BEGIN PGP SIGNATURE-----  
Version: GnuPG v1.4.8 (Darwin)

iEYEARECAAYFAkfrk3MACgkQJA/zY0IIRZZImgCeLw1Db8i/wyhlNP+0jrlXpjUD  
RaUAn0ecOSw8Qd5wOzmwI/LHkL5rBncb  
=/pKG  
-----END PGP SIGNATURE-----

---

<div class="post-metadata">

**Author:** ![selva](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/13edae/32.png) [@selva](https://www.ruby-forum.com/u/selva)\
**Post date:** [March 27, 2008, 2:31pm UTC](https://www.ruby-forum.com/t/over-loading/133055/5 "2008-03-27T14:31:47Z")

</div>

2008/3/27, Selvaraj S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> I think we can pass any number of argument as an array to method. is  
> this the reason or any other ???

Better than an Array you can pass a Hash , so you keep the variable  
name:

objet.my\_method( { “num”=\>2313 , “name”=\>“My Name”,  
“phone”=\>“+34232343243” } )

def my\_method ( variables = {} )  
puts “My name is” + variables[“name”]  
puts “My id num is” + variables[“num”].to\_i  
end

AFAIK RubyOnRails uses this way a lot.
