method overloading?

Discussion in 'Ruby' started by Donglai Gong, Jun 27, 2003.

  1. Donglai Gong

    Donglai Gong Guest

    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 Gong, Jun 27, 2003
    1. Advertisements

  2. Donglai Gong

    Ryan Pavlik Guest

    <shameless plug>

    The StrongTyping module on RAA lets you do this:

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

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


    overload_default args

    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, Jun 27, 2003
    1. Advertisements

    Hash: SHA1

    Ruby Application Archive.

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

    - --
    Daniel Carrera | OpenPGP fingerprint:
    Graduate TA, Math Dept | 6643 8C8B 3522 66CB D16C D779 2FDD 7DAC 9AF7 7A88
    UMD (301) 405-5137 |
    Version: GnuPG v1.2.2 (SunOS)

    -----END PGP SIGNATURE-----
    Daniel Carrera, Jun 27, 2003
  4. Robert Klemme, Jun 27, 2003
  5. On 0627, Ryan Pavlik wrote:

    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.

    KONTRA Gergely, Jun 27, 2003
  6. Donglai Gong

    nobu.nokada Guest


    At Fri, 27 Jun 2003 22:42:42 +0900,

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

    def foo(arg1, arg2)
    overload:)foo, String, String)
    nobu.nokada, Jun 27, 2003
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.