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

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

    Ruby Application Archive.

    http://raa.ruby-lang.org

    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 | http://www.math.umd.edu/~dcarrera/pgp.html
    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.2.2 (SunOS)

    iD8DBQE++9dcnxE8DWHf+OcRAvQDAKCHurhslqK8ufDgOGEhpWNqqW4NlACcCXFq
    riuo3zsuczQp2ldBuDzbvu0=
    =btKo
    -----END PGP SIGNATURE-----
     
    Daniel Carrera, Jun 27, 2003
    #3
  4. Robert Klemme, Jun 27, 2003
    #4
  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.

    Gergo
     
    KONTRA Gergely, Jun 27, 2003
    #5
  6. Donglai Gong

    nobu.nokada Guest

    Hi,

    At Fri, 27 Jun 2003 22:42:42 +0900,
    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)
     
    nobu.nokada, Jun 27, 2003
    #6
    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.