newbie question: function overloading

  • Thread starter Dimitrios Galanakis
  • Start date
D

Dimitrios Galanakis

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
 
R

Robert Klemme

Dimitrios Galanakis said:
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?MethodOverloading

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

Regards

robert
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,045
Latest member
DRCM

Latest Threads

Top