From: Yang Bob Date: 2006-12-04T13:07:53+09:00 Subject: Re: method overloading like Java? ------=_Part_33476_26714860.1165205269529 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline But how about overloading by parameter numbers. such as: class FooClass { def foo(first){ } def foo(first, second) { } } does ruby support this ? if can not, how can we workaround this? thanks 2006/12/4, Pat Maddox : > > On 12/3/06, Gordon Thiesfeld wrote: > > > Is there a way to do this in Ruby? I am aware of method(*args) and > > > case...when but I think Java's version is cleaner and more > readable. I > > > also know that variables in Ruby are dynamically typed, the method > that > > > is called should be based on the most recent type of the variable > being > > > passed. > > > > What about this? > > > > class String > > def foo > > puts 'String' > > end > > end > > > > class Numeric > > def foo > > puts 'Numeric' > > end > > end > > > > x1 = 1 > > x2 = 'string' > > > > x1.foo > > x2.foo > > EXACTLY > > If you want different types of objects to perform different behavior > for the same method call, you use polymorphism. What OP wants to do > is essentially a design pattern for providing polymorphism > capabilities to a language that doesn't have them built in. > > Pat > > ------=_Part_33476_26714860.1165205269529--