I think overloading is very much attached to static typing and not as useful in dynamic languages as it seems. Creating more methods is free and generally more expressive. Polymorphism is implicit (look up what we call 'duck-typing' here). I find that I rarely have the need for overloading in Ruby (or that I overload all the time, depending on which way you look at it - no type signatures means that you can pass in anything at all) - and when I need it, I use the rails-kind named arguments.
Mostly, I think instead of creating several methods of the same name, Ruby
programmers tend to create methods with different names. Some of the use
cases of overloading can also be addressed with optional arguments, trailing
hash arguments, and so on.
···
On Sun, Apr 18, 2010 at 10:53 PM, Derek Cannon <novellterminator@gmail.com> wrote:
How do Ruby programmers handle method overloading? In Java, I could
easily create several methods of the same name that accept a variety of
input.
I know in Ruby, using *args you can accept an unlimited number of
parameters. Do I just this with a series of if statements?
maybe you could show us a sample use case of method overloading since
i cannot think of one right now
Sure. A simple example of what I wanted to do (using method overloading
in this example for lack of better know-how):
Class XYZ
def initialize(title, days, time, professor) @title = title @days = days @time = time @professor = professor
end
def initialize(title, days, time, professor, lab_time, lab_days) @title = title @days = days @time = time @professor = professor @lab_time = lab_time @lab_days = lab_days
end
Class XYZ
def initialize(title, days, time, professor, lab_time=nil, lab_days=nil) @title = title @days = days @time = time @professor = professor @lab_time = lab_time @lab_days = lab_days
end
end
Jesus.
···
On Mon, Apr 19, 2010 at 10:39 AM, Derek Cannon <novellterminator@gmail.com> wrote:
maybe you could show us a sample use case of method overloading since
i cannot think of one right now
Sure. A simple example of what I wanted to do (using method overloading
in this example for lack of better know-how):