From: "Jesús Gabriel y Galán" Date: 2010-01-28T00:43:07+09:00 Subject: Re: Class with Multi Constrcutor ( initialize ) On Wed, Jan 27, 2010 at 4:34 PM, Mido Peace wrote: > Hey .. > I'm Tryin' to port Some Java Module into Ruby , but I have a small > probleme , > how can I do to declare multi 'initialize Method' depending of the > number of user > argument > > i.e in Java we can have a Class with many constructor > class myClass { > >   //... >    public MyClass () { ... }  // Defualt One >    public MyClass (Object obj1 ) { ... } >    public myClass  ( Object obj1,Object obj2 ...,Object objn) { ... } >  // ... > } > > I tried : > class myClass >   def initialize () >     // ... >   end > >   def intialize ( value ) >    // ... >   end > end > > but doesnt works ! > > I just wanna find a way to call the right constructor ( or initializer) > depending of the number of arguments Ruby doesn't support method overloading. If you need different implementations just depending on the number of arguments you can do: class MyClass def initialize *args case args.size when 1 _init_1_param *args when 2 _init_2_params *args .... end end or you can do this: class MyClass def initialize(options = {}) # and have the logic depend on the keys present in the hash end end So you can call: MyClass.new MyClass.new(:obj1 => some_object, :obj2 => some_other_object) Hope this helps, Jesus.