Method overloading

Hi all,

I think I guess the answer to this question, but I’d like to know if I’m
wrong or I’m missing something.

I wanna ask you about method overloading. You can overload methods based on
the number of parameters but… what about overloading based on parameter’s
classes? Something like (in C++):

int test(int a)
int test(char* a)

Is there any way or must I write some code like: if (a.class == ‘class’)
then…?

I hope I explained right :wink:

Thank you.

···


Imobach González Sosa

imodev@softhome.net schrieb im Newsbeitrag
news:courier.40A8A6A3.00004CC5@softhome.net

Hi all,

I think I guess the answer to this question, but I’d like to know if I’m
wrong or I’m missing something.

I wanna ask you about method overloading. You can overload methods based
on
the number of parameters but… what about overloading based on
parameter’s
classes? Something like (in C++):

int test(int a)
int test(char* a)

Is there any way or must I write some code like: if (a.class == ‘class’)
then…?

It’s not built into the language, but there are some resources (text and
code) out there:

For more questions about C++ like Ruby:

I hope I explained right :wink:

Perfectly.

Kind regards

robert

imodev@softhome.net wrote…

… what about overloading based on parameter’s
classes? Something like (in C++):

int test(int a)
int test(char* a)

Is there any way or must I write some code like: if (a.class == ‘class’)
then…?

Yes. The class method returns a Class object. So you can compare:
a.class <= Integer
to check if a is an Integer or any subclass of Integer.

Better, you can use the kind_of? method:
a.kind_of?(Integer)
which is more obvious.

But for this situation, a case statement is probably best:

def test(a)
case a
when Integer
# …
when String
# …
else
raise ArgumentError, “test(a): parameter must be Integer or String”,
caller
end
end

Robert Klemme writes:

It’s not built into the language, but there are some resources (text and
code) out there:
http://www.rubygarden.org/ruby?MethodOverloading

For more questions about C++ like Ruby:
http://www.rubygarden.org/ruby?RubyFromCpp

Just was I looking for. Thank you for your quick answer :wink:

I hope I explained right :wink:

Perfectly.

I fear language “issues” :slight_smile:

Thanks. See ya!

···


Imobach González Sosa

“Dave Burt” burtdav@hotmail.com schrieb im Newsbeitrag
news:Ew3qc.43766$TT.3729@news-server.bigpond.net.au…

imodev@softhome.net wrote…

… what about overloading based on parameter’s
classes? Something like (in C++):

int test(int a)
int test(char* a)

Is there any way or must I write some code like: if (a.class ==
‘class’)
then…?

Yes. The class method returns a Class object. So you can compare:
a.class <= Integer
to check if a is an Integer or any subclass of Integer.

Better, you can use the kind_of? method:
a.kind_of?(Integer)
which is more obvious.

And then there is the third idiom:

Integer === a

which btw nicely integrates in case statements as you show below:

But for this situation, a case statement is probably best:

def test(a)
case a
when Integer
# …
when String
# …
else
raise ArgumentError, “test(a): parameter must be Integer or
String”,
caller
end
end

Btw: there’s a gotcha with the idiom on the wiki:

def fred(*args)
case args.collect { |a| a.type}
when [Float, Fixnum, String]
f, i, s = args
# …

This will not use Class#=== i.e. match only if the arguments’ types match
identical (instead of sub classes matching with superclasses for all the
three idioms listed above).

Kind regards

robert

“Robert Klemme” bob.news@gmx.net wrote…

And then there is the third idiom:

Integer === a

Btw: there’s a gotcha with the idiom on the wiki:

def fred(*args)
case args.collect { |a| a.type}
when [Float, Fixnum, String]
f, i, s = args
# …

This will not use Class#=== i.e. match only if the arguments’ types match
identical (instead of sub classes matching with superclasses for all the
three idioms listed above).

My omissions were for simplicity; thanks for expanding, Robert.

Further, just after posting this, I read Tim Bates on duck typing
[ruby-talk:100516], and was convicted.

A well-designed program will usually not need to do that kind of thing.
Method overloading like that is a very static-typing thing to do. The Ruby
Way often obviates the need for such checks.

“Dave Burt” burtdav@hotmail.com schrieb im Newsbeitrag
news:V94qc.43824$TT.14932@news-server.bigpond.net.au…

“Robert Klemme” bob.news@gmx.net wrote…

And then there is the third idiom:

Integer === a

Btw: there’s a gotcha with the idiom on the wiki:

def fred(*args)
case args.collect { |a| a.type}
when [Float, Fixnum, String]
f, i, s = args
# …

This will not use Class#=== i.e. match only if the arguments’ types
match
identical (instead of sub classes matching with superclasses for all
the
three idioms listed above).

My omissions were for simplicity; thanks for expanding, Robert.

Further, just after posting this, I read Tim Bates on duck typing
[ruby-talk:100516], and was convicted.

A well-designed program will usually not need to do that kind of thing.
Method overloading like that is a very static-typing thing to do. The
Ruby
Way often obviates the need for such checks.

Yeah, true. And if different argument lists lead to different behavior,
then it might be better to use different method names anyway.

Regards

robert