Method overloading?

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

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

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 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

“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

“Donglai Gong” 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:

robert

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
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.2 (SunOS)

iD8DBQE++8xOnxE8DWHf+OcRAl4DAKDRbjvOrfMXCThfwDS2ByiyuuxsbwCeLJNT
kqbKGOYm5OUjay6wiiHtwjU=
=kyCD
-----END PGP SIGNATURE-----

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 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

“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

[…]

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, Gergelykgergely@mcl.hu PhD student Room IB113 ]---------+

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 --+

Refactored and added some content:

···

On Friday, Jun 27, 2003, at 03:03 America/Chicago, Robert Klemme wrote:

“Donglai Gong” 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?KeywordArguments


John Platte
Principal Consultant, NIKA Consulting
http://nikaconsulting.com/

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
-----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

Ruby Application Archive.

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 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

“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

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

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

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