[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

newbie question: function overloading

Dimitrios Galanakis

10/2/2003 6:44:00 PM

I need to define a method that performs differently when operated on objects
of different type (overloading). Currently I use various if's to check for
the type of the object as follows:

def somefunction(a)

if a.kind_of?(someType)
expression1
return
end

if a.kind_of(someOtherType)
expression2
return

end


i am wondering if there is a simpler way to implement that without the if's,
in a way similar to C++ overloading. I mean I would like to define two
functions with the same name that will somehow be aware of the type of
their argument...

sincerely,

DG

2 Answers

Oliver Dain

10/2/2003 10:25:00 PM

0

Dimitrios Galanakis wrote:

> I need to define a method that performs differently when operated on
> objects of different type (overloading). Currently I use various
> if's to check for the type of the object as follows:
>
> def somefunction(a)
>
> if a.kind_of?(someType)
> expression1
> return
> end
>
> if a.kind_of(someOtherType)
> expression2
> return
>
> end
>
>
> i am wondering if there is a simpler way to implement that without
> the if's, in a way similar to C++ overloading. I mean I would like
> to define two functions with the same name that will somehow be
> aware of the type of their argument...
>
> sincerely,
>
> DG

Check out the strongtyping module at RAA:

http://raa.ruby-lang.org/list.rhtml?name=st...

Robert Klemme

10/6/2003 8:17:00 AM

0


"Dimitrios Galanakis" <galanaki@uiuc.edu> schrieb im Newsbeitrag
news:4i_eb.147$fm2.69960@vixen.cso.uiuc.edu...
> I need to define a method that performs differently when operated on
objects
> of different type (overloading). Currently I use various if's to check
for
> the type of the object as follows:
>
> def somefunction(a)
>
> if a.kind_of?(someType)
> expression1
> return
> end
>
> if a.kind_of(someOtherType)
> expression2
> return
>
> end
>
>
> i am wondering if there is a simpler way to implement that without the
if's,
> in a way similar to C++ overloading. I mean I would like to define two
> functions with the same name that will somehow be aware of the type of
> their argument...

You can find further info here:
http://www.rubygarden.org/ruby?MethodO...

This might be of interest, too:
http://www.rubygarden.org/ruby?Keywor...

Regards

robert