Forum: Ruby on Rails Does rails support method overloading?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Fcdafe495dccc04d27f6dbbcdf54aecc?d=identicon&s=25 Woei Shyang (generik)
on 2006-01-12 08:00
I've tried to have 2 methods of the same name (but with different
parameter signatures) and it always seems like the later declaration
tends to win out, does this mean that ruby does not support overloading?

If so what aboutt the .new method? You can either call it as .new (with
nothing) or .new(hash variable). Is it possible to override a specific
instance of the zero parameter constructor using this fact?

Thanks!
17acfbd3a05cfb59e0543235cc0a813b?d=identicon&s=25 Joshua Susser (Guest)
on 2006-01-12 08:28
Woei Shyang wrote:
> I've tried to have 2 methods of the same name (but with different
> parameter signatures) and it always seems like the later declaration
> tends to win out, does this mean that ruby does not support overloading?

Ruby does not support C++-style  method overloading (what OOP theorists
call "multi-methods"). Instead it uses Smalltalk-style messaging, where
only the receiver of the message is consulted to find the method that
implements the message.

> If so what aboutt the .new method? You can either call it as .new (with
> nothing) or .new(hash variable). Is it possible to override a specific
> instance of the zero parameter constructor using this fact?

Ruby has a couple ways that the apparent signature of a message can
vary, but it's always the same message with the same signature. One way
is that Ruby allows for default values for parameters. If you declare a
method:

def deposit(amount = 100)
...

Then you can call it as deposit(250), or as deposit() and it will use
the default value of 100 as the amount.

The other thing is the last argument of a method can accept a
variable-length list of values or a hash. All these features provide a
lot of flexibility, but it's all just one method.

If you want to do something akin to multi-methods, use double
dispatching.

joshua susser
This topic is locked and can not be replied to.