I've run into this before when (for instance) I want initialize() to
do one thing if it gets one arg, and a totally different thing if it
gets two args. AFAIK, the only way to do this is to bundle the args up
in an array, and to look at the size of that, which just feels clumsy.

-Eliah.


On Tue, 22 Feb 2005 06:31:00 +0900, James G. Britt
<ruby.talk.list / gmail.com> wrote:
> On Tue, 22 Feb 2005 06:04:07 +0900, Panagiotis Karvounis
> <pkarvou / gmail.com> wrote:
> > Hi,
> >
> > Does Ruby support constructor overloading?
> 
> No; Ruby desn't support method overloading.  But you can pass a
> variable number of arguments and do some logic based on what's given.
> 
> One approach is to define all the args and set default values:
> 
>  def initialize ( x, y="Hey!", z=nil )
>  end
> 
> Another might be to just accept any number of args as an array:
> 
>  def initialize ( *args)
>   # The array args now has the arguments
>  end
> 
> Or combine them
> 
>  def initialize (x=0, y="Hey!", *z=nil )
>  end
> 
> Or use a hash as the primary argument:
> 
>  def initialize (  h={} )
>   # Now go look for named arguments in the hash
>  end
> 
> f = Foo.new( :user_name => "Jimbo!",  :country => "Bronx" )
> 
> What are you trying to accomplish?
> 
> 
> James
> 
>