# Method overloading?

**URL:** <https://rubytalk.org/t/method-overloading/6588>\
**Category:** ruby-talk\
**Created:** [27 June 2003 04:43 UTC](https://rubytalk.org/t/method-overloading/6588 "2003-06-27T04:43:42Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Donglai\_Gong](https://avatars.discourse-cdn.com/v4/letter/d/e19adc/32.png) [@Donglai\_Gong](https://rubytalk.org/u/Donglai_Gong)\
**Post date:** [27 June 2003 04:43 UTC](https://rubytalk.org/t/method-overloading/6588/1 "2003-06-27T04:43:42Z")

</div>

Is there a way to implement method overloading in ruby? Naively I just  
defined two methods with the same name with different number of  
parameters…Ruby didn’t like that.

Donglai

---

<div class="post-metadata">

**Author:** ![Daniel\_Carrera](https://avatars.discourse-cdn.com/v4/letter/d/c67d28/32.png) [@Daniel\_Carrera](https://rubytalk.org/u/Daniel_Carrera)\
**Post date:** [27 June 2003 04:47 UTC](https://rubytalk.org/t/method-overloading/6588/2 "2003-06-27T04:47:17Z")

</div>

Does this help?:

def my\_method(\*args)  
if args.length == 2  
two\_parameters\_method(args)  
elsif args.length == 3  
three\_parameters\_method(args)  
end  
end

> **···**
>
> On Fri, Jun 27, 2003 at 01:43:42PM +0900, Donglai Gong wrote:
> 
> > Is there a way to implement method overloading in ruby? Naively I just  
> > defined two methods with the same name with different number of  
> > parameters…Ruby didn’t like that.
> > 
> > Donglai
> 
> * * *
> 
> Daniel Carrera | OpenPGP fingerprint:  
> Graduate TA, Math Dept | 6643 8C8B 3522 66CB D16C D779 2FDD 7DAC 9AF7 7A88  
> UMD (301) 405-5137 | [http://www.math.umd.edu/~dcarrera/pgp.html](http://www.math.umd.edu/~dcarrera/pgp.html)

---

<div class="post-metadata">

**Author:** ![Ryan\_Pavlik1](https://avatars.discourse-cdn.com/v4/letter/r/7c8e57/32.png) [@Ryan\_Pavlik1](https://rubytalk.org/u/Ryan_Pavlik1)\
**Post date:** [27 June 2003 05:21 UTC](https://rubytalk.org/t/method-overloading/6588/3 "2003-06-27T05:21:06Z")

</div>

The StrongTyping module on RAA lets you do this:

```
def foo(*args)
    overload(args, String) {
        ...
        return ...
    }

    overload(args, String, String) {
        ...
        return ...
    }

    ...

    overload_default args
end

```

Of course, if you don’t care what type of object you want  
specifically, you can just say “Object”, and you can specify multiple  
types like [String, NilClass].

\</shameless plug\>

> **···**
>
> On Fri, 27 Jun 2003 13:43:42 +0900 Donglai Gong [donglai@MIT.EDU](mailto:donglai@MIT.EDU) wrote:
> 
> > Is there a way to implement method overloading in ruby? Naively I just  
> > defined two methods with the same name with different number of  
> > parameters…Ruby didn’t like that.
> > 
> > Donglai
> 
> –  
> Ryan Pavlik [rpav@users.sf.net](mailto:rpav@users.sf.net)
> 
> “Ye don’t like me? But we don’t even know each other. I  
> bets we have a lot in common. Treachery and such. Yar.” - 8BT

---

<div class="post-metadata">

**Author:** ![Robert](https://avatars.discourse-cdn.com/v4/letter/r/e95f7d/32.png) [@Robert](https://rubytalk.org/u/Robert)\
**Post date:** [27 June 2003 08:03 UTC](https://rubytalk.org/t/method-overloading/6588/4 "2003-06-27T08:03:59Z")

</div>

“Donglai Gong” [donglai@MIT.EDU](mailto:donglai@MIT.EDU) schrieb im Newsbeitrag  
news:EBA4EC11-A859-11D7-9203-000393DA182A@mit.edu…

> Is there a way to implement method overloading in ruby? Naively I just  
> defined two methods with the same name with different number of  
> parameters…Ruby didn’t like that.

It’s on the Wiki:

> **[WebMD - Better information. Better health.](https://www.webmd.com/?RubyFromCpp)**
>
> The leading source for trustworthy and timely health and medical news and information. Providing credible health information, supportive community, and educational services by blending award-winning expertise in content, community services, expert...

> **[WebMD - Better information. Better health.](https://www.webmd.com/?KeywordArguments)**
>
> The leading source for trustworthy and timely health and medical news and information. Providing credible health information, supportive community, and educational services by blending award-winning expertise in content, community services, expert...

```
robert

```

---

<div class="post-metadata">

**Author:** ![Donglai\_Gong](https://avatars.discourse-cdn.com/v4/letter/d/e19adc/32.png) [@Donglai\_Gong](https://rubytalk.org/u/Donglai_Gong)\
**Post date:** [27 June 2003 05:12 UTC](https://rubytalk.org/t/method-overloading/6588/5 "2003-06-27T05:12:47Z")

</div>

Yes, this would solve the problem but it’s not ideal or elegant, the  
code is now much less obvious.

For example:

class Bank  
def open(safe, combination)  
…  
end

def open(window)  
…  
end  
end

is a lot more revealing than:

class Bank  
def open(\*args)  
if args.length == 1  
…  
elsif args.length == 2  
…  
end  
end  
end

Thanks for the help though, it just feels like overloading could be  
done in a simpler manner.

Donglai

> **···**
>
> On Friday, Jun 27, 2003, at 00:47 US/Eastern, Daniel Carrera wrote:
> 
> > -----BEGIN PGP SIGNED MESSAGE-----  
> > Hash: SHA1
> > 
> > Does this help?:
> > 
> > def my\_method(\*args)  
> > if args.length == 2  
> > two\_parameters\_method(args)  
> > elsif args.length == 3  
> > three\_parameters\_method(args)  
> > end  
> > end
> > 
> > On Fri, Jun 27, 2003 at 01:43:42PM +0900, Donglai Gong wrote:
> > 
> > > Is there a way to implement method overloading in ruby? Naively I  
> > > just  
> > > defined two methods with the same name with different number of  
> > > parameters…Ruby didn’t like that.
> > > 
> > > Donglai
> > 
> > * * *
> > 
> > Daniel Carrera | OpenPGP fingerprint:  
> > Graduate TA, Math Dept | 6643 8C8B 3522 66CB D16C D779 2FDD 7DAC 9AF7  
> > 7A88  
> > UMD (301) 405-5137 | [http://www.math.umd.edu/~dcarrera/pgp.html](http://www.math.umd.edu/~dcarrera/pgp.html)  
> > -----BEGIN PGP SIGNATURE-----  
> > Version: GnuPG v1.2.2 (SunOS)
> > 
> > iD8DBQE++8xOnxE8DWHf+OcRAl4DAKDRbjvOrfMXCThfwDS2ByiyuuxsbwCeLJNT  
> > kqbKGOYm5OUjay6wiiHtwjU=  
> > =kyCD  
> > -----END PGP SIGNATURE-----

---

<div class="post-metadata">

**Author:** ![Donglai\_Gong](https://avatars.discourse-cdn.com/v4/letter/d/e19adc/32.png) [@Donglai\_Gong](https://rubytalk.org/u/Donglai_Gong)\
**Post date:** [27 June 2003 05:31 UTC](https://rubytalk.org/t/method-overloading/6588/6 "2003-06-27T05:31:09Z")

</div>

What is RAA? Is there any documentation for it? Thanks.

Donglai

> **···**
>
> On Friday, Jun 27, 2003, at 01:21 US/Eastern, Ryan Pavlik wrote:
> 
> > On Fri, 27 Jun 2003 13:43:42 +0900 \> Donglai Gong [donglai@MIT.EDU](mailto:donglai@MIT.EDU) wrote:
> > 
> > > Is there a way to implement method overloading in ruby? Naively I  
> > > just  
> > > defined two methods with the same name with different number of  
> > > parameters…Ruby didn’t like that.
> > > 
> > > Donglai
> > 
> > The StrongTyping module on RAA lets you do this:
> > 
> > ```
> > def foo(*args)
> > overload(args, String) {
> > ...
> > return ...
> > }
> > 
> > overload(args, String, String) {
> > ...
> > return ...
> > }
> > 
> > ...
> > 
> > overload_default args
> > end
> > 
> > ```
> > 
> > Of course, if you don’t care what type of object you want  
> > specifically, you can just say “Object”, and you can specify multiple  
> > types like [String, NilClass].
> > 
> > \</shameless plug\>
> > 
> > –  
> > Ryan Pavlik [rpav@users.sf.net](mailto:rpav@users.sf.net)
> > 
> > “Ye don’t like me? But we don’t even know each other. I  
> > bets we have a lot in common. Treachery and such. Yar.” - 8BT

---

<div class="post-metadata">

**Author:** ![KONTRA\_Gergely](https://avatars.discourse-cdn.com/v4/letter/k/3bc359/32.png) [@KONTRA\_Gergely](https://rubytalk.org/u/KONTRA_Gergely)\
**Post date:** [27 June 2003 13:42 UTC](https://rubytalk.org/t/method-overloading/6588/7 "2003-06-27T13:42:42Z")

</div>

[…]

> The StrongTyping module on RAA lets you do this:
> 
> ```
> def foo(*args)
> overload(args, String) {
> ...
> return ...
> }
> 
> overload(args, String, String) {
> ...
> return ...
> }
> 
> ...
> 
> overload_default args
> end
> 
> ```

[…]

But this also doesn’t allow one to later add a new case…  
I guess it needs some help from the core of the language to work…  
BTW I think dynamic type checking fits to the ruby design.

Gergo

> **···**
>
> On 0627, Ryan Pavlik wrote:
> 
> –  
> ±[Kontra, Gergely[kgergely@mcl.hu](mailto:kgergely@mcl.hu) PhD student Room IB113 ]---------+
> 
> > [http://www.mcl.hu/~kgergely](http://www.mcl.hu/~kgergely) “Olyan langesz vagyok, hogy |  
> > Mobil:(+36 20) 356 9656 ICQ: 175564914 poroltoval kellene jarnom” |  
> > ±- Magyar php mirror es magyar php dokumentacio: [http://hu.php.net](http://hu.php.net) --+

---

<div class="post-metadata">

**Author:** ![John\_Platte](https://avatars.discourse-cdn.com/v4/letter/j/9de053/32.png) [@John\_Platte](https://rubytalk.org/u/John_Platte)\
**Post date:** [27 June 2003 14:34 UTC](https://rubytalk.org/t/method-overloading/6588/8 "2003-06-27T14:34:44Z")

</div>

Refactored and added some content:

> **[WebMD - Better information. Better health.](https://www.webmd.com/?MethodOverloading)**
>
> The leading source for trustworthy and timely health and medical news and information. Providing credible health information, supportive community, and educational services by blending award-winning expertise in content, community services, expert...

> **···**
>
> On Friday, Jun 27, 2003, at 03:03 America/Chicago, Robert Klemme wrote:
> 
> > “Donglai Gong” [donglai@MIT.EDU](mailto:donglai@MIT.EDU) schrieb im Newsbeitrag  
> > news:EBA4EC11-A859-11D7-9203-000393DA182A@mit.edu…
> > 
> > > Is there a way to implement method overloading in ruby? Naively I  
> > > just  
> > > defined two methods with the same name with different number of  
> > > parameters…Ruby didn’t like that.
> > 
> > It’s on the Wiki:  
> > [http://www.rubygarden.org/ruby?RubyFromCpp](http://www.rubygarden.org/ruby?RubyFromCpp)  
> > [http://www.rubygarden.org/ruby?KeywordArguments](http://www.rubygarden.org/ruby?KeywordArguments)
> 
> –  
> John Platte  
> Principal Consultant, NIKA Consulting  
> [http://nikaconsulting.com/](http://nikaconsulting.com/)

---

<div class="post-metadata">

**Author:** ![Daniel\_Carrera](https://avatars.discourse-cdn.com/v4/letter/d/c67d28/32.png) [@Daniel\_Carrera](https://rubytalk.org/u/Daniel_Carrera)\
**Post date:** [27 June 2003 05:22 UTC](https://rubytalk.org/t/method-overloading/6588/9 "2003-06-27T05:22:53Z")

</div>

Well, I guess that using a case statement would help a little.

Maybe there is a really simple way of doing this and I just don’t know  
it.

> **···**
>
> On Fri, Jun 27, 2003 at 02:12:47PM +0900, Donglai Gong wrote:
> 
> > Yes, this would solve the problem but it’s not ideal or elegant, the  
> > code is now much less obvious.
> > 
> > For example:
> > 
> > class Bank  
> > def open(safe, combination)  
> > …  
> > end
> > 
> > def open(window)  
> > …  
> > end  
> > end
> > 
> > is a lot more revealing than:
> > 
> > class Bank  
> > def open(\*args)  
> > if args.length == 1  
> > …  
> > elsif args.length == 2  
> > …  
> > end  
> > end  
> > end
> > 
> > Thanks for the help though, it just feels like overloading could be  
> > done in a simpler manner.
> > 
> > Donglai
> > 
> > On Friday, Jun 27, 2003, at 00:47 US/Eastern, Daniel Carrera wrote:
> > 
> > > -----BEGIN PGP SIGNED MESSAGE-----  
> > > Hash: SHA1
> > > 
> > > Does this help?:
> > > 
> > > def my\_method(\*args)  
> > > if args.length == 2  
> > > two\_parameters\_method(args)  
> > > elsif args.length == 3  
> > > three\_parameters\_method(args)  
> > > end  
> > > end
> > > 
> > > On Fri, Jun 27, 2003 at 01:43:42PM +0900, Donglai Gong wrote:
> > > 
> > > > Is there a way to implement method overloading in ruby? Naively I  
> > > > just  
> > > > defined two methods with the same name with different number of  
> > > > parameters…Ruby didn’t like that.
> > > > 
> > > > Donglai
> > > 
> > > * * *
> > > 
> > > Daniel Carrera | OpenPGP fingerprint:  
> > > Graduate TA, Math Dept | 6643 8C8B 3522 66CB D16C D779 2FDD 7DAC 9AF7  
> > > 7A88  
> > > UMD (301) 405-5137 | [http://www.math.umd.edu/~dcarrera/pgp.html](http://www.math.umd.edu/~dcarrera/pgp.html)  
> > > -----BEGIN PGP SIGNATURE-----  
> > > Version: GnuPG v1.2.2 (SunOS)
> > > 
> > > iD8DBQE++8xOnxE8DWHf+OcRAl4DAKDRbjvOrfMXCThfwDS2ByiyuuxsbwCeLJNT  
> > > kqbKGOYm5OUjay6wiiHtwjU=  
> > > =kyCD  
> > > -----END PGP SIGNATURE-----
> 
> * * *
> 
> Daniel Carrera | OpenPGP fingerprint:  
> Graduate TA, Math Dept | 6643 8C8B 3522 66CB D16C D779 2FDD 7DAC 9AF7 7A88  
> UMD (301) 405-5137 | [http://www.math.umd.edu/~dcarrera/pgp.html](http://www.math.umd.edu/~dcarrera/pgp.html)

---

<div class="post-metadata">

**Author:** ![Daniel\_Carrera](https://avatars.discourse-cdn.com/v4/letter/d/c67d28/32.png) [@Daniel\_Carrera](https://rubytalk.org/u/Daniel_Carrera)\
**Post date:** [27 June 2003 05:34 UTC](https://rubytalk.org/t/method-overloading/6588/10 "2003-06-27T05:34:26Z")

</div>

Ruby Application Archive.

[http://raa.ruby-lang.org](http://raa.ruby-lang.org)

It’s analogous to CPAN, CTAN and the Vaults.

> **···**
>
> On Fri, Jun 27, 2003 at 02:31:09PM +0900, Donglai Gong wrote:
> 
> > What is RAA? Is there any documentation for it? Thanks.
> > 
> > Donglai
> > 
> > On Friday, Jun 27, 2003, at 01:21 US/Eastern, Ryan Pavlik wrote:
> > 
> > > On Fri, 27 Jun 2003 13:43:42 +0900 \> \>Donglai Gong [donglai@MIT.EDU](mailto:donglai@MIT.EDU) wrote:
> > > 
> > > > Is there a way to implement method overloading in ruby? Naively I  
> > > > just  
> > > > defined two methods with the same name with different number of  
> > > > parameters…Ruby didn’t like that.
> > > > 
> > > > Donglai
> > > 
> > > The StrongTyping module on RAA lets you do this:
> > > 
> > > def foo(\*args)  
> > > overload(args, String) {  
> > > …  
> > > return …  
> > > }
> > > 
> > > ```
> > > overload(args, String, String) {
> > > ...
> > > return ...
> > > }
> > > 
> > > ...
> > > 
> > > overload_default args
> > > 
> > > ```
> > > 
> > > end
> > > 
> > > Of course, if you don’t care what type of object you want  
> > > specifically, you can just say “Object”, and you can specify multiple  
> > > types like [String, NilClass].
> > > 
> > > \</shameless plug\>
> > > 
> > > –  
> > > Ryan Pavlik [rpav@users.sf.net](mailto:rpav@users.sf.net)
> > > 
> > > “Ye don’t like me? But we don’t even know each other. I  
> > > bets we have a lot in common. Treachery and such. Yar.” - 8BT
> 
> * * *
> 
> Daniel Carrera | OpenPGP fingerprint:  
> Graduate TA, Math Dept | 6643 8C8B 3522 66CB D16C D779 2FDD 7DAC 9AF7 7A88  
> UMD (301) 405-5137 | [http://www.math.umd.edu/~dcarrera/pgp.html](http://www.math.umd.edu/~dcarrera/pgp.html)

---

<div class="post-metadata">

**Author:** ![Mauricio\_Fernndez](https://avatars.discourse-cdn.com/v4/letter/m/e495f1/32.png) [@Mauricio\_Fernndez](https://rubytalk.org/u/Mauricio_Fernndez)\
**Post date:** [27 June 2003 06:00 UTC](https://rubytalk.org/t/method-overloading/6588/11 "2003-06-27T06:00:20Z")

</div>

Why not  
class Bank  
def open\_safe(safe, combination)  
…  
end

```
def open_window(window)
...
end

```

end

I use this for constructors all the time.

> **···**
>
> On Fri, Jun 27, 2003 at 02:12:47PM +0900, Donglai Gong wrote:
> 
> > Yes, this would solve the problem but it’s not ideal or elegant, the  
> > code is now much less obvious.
> > 
> > For example:
> > 
> > class Bank  
> > def open(safe, combination)  
> > …  
> > end
> > 
> > def open(window)  
> > …  
> > end  
> > end
> 
> –  
> \_ \_
> 
> > > \_\_ \_\_ _| |_ \_\_\_ \_ \_\_ \_\_\_ \_\_ \_ \_ \_\_  
> > > '\_ \ / _`| __/__ | '_` \_ \ / ` | ’ \   
> > > ) | (| | |_\_\_ \ | | | | | (_| | | | |  
> > > .\_\_/ \_,_|\ __|__ /| |_| |_|\__,_|_| |\_|  
> > > Running Debian GNU/Linux Sid (unstable)  
> > > batsman dot geo at yahoo dot com
> 
> ‘Ooohh… “FreeBSD is faster over loopback, when compared to Linux  
> over the wire”. Film at 11.’  
> – Linus Torvalds

---

<div class="post-metadata">

**Author:** ![Nobuyoshi\_Nakada](https://avatars.discourse-cdn.com/v4/letter/n/edb3f5/32.png) [@Nobuyoshi\_Nakada](https://rubytalk.org/u/Nobuyoshi_Nakada)\
**Post date:** [27 June 2003 13:55 UTC](https://rubytalk.org/t/method-overloading/6588/12 "2003-06-27T13:55:08Z")

</div>

Hi,

> But this also doesn’t allow one to later add a new case…  
> I guess it needs some help from the core of the language to work…

[http://raa.ruby-lang.org/list.rhtml?name=overload](http://raa.ruby-lang.org/list.rhtml?name=overload)

```
def foo(arg)
end
overload(:foo, String)

def foo(arg1, arg2)
end
overload(:foo, String, String)

```

> **···**
>
> At Fri, 27 Jun 2003 22:42:42 +0900, KONTRA Gergely wrote:
> 
> –  
> Nobu Nakada
