From: Pat Maddox Date: 2006-12-04T12:50:45+09:00 Subject: Re: method overloading like Java? 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