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
--
Posted via http://www.ruby-forum.com/.