# Multiple constructors?

**URL:** <https://www.ruby-forum.com/t/multiple-constructors/143910>\
**Category:** Ruby\
**Created:** [July 23, 2008, 7:20pm UTC](https://www.ruby-forum.com/t/multiple-constructors/143910 "2008-07-23T19:20:10Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![aidy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/eada6e/32.png) [@aidy](https://www.ruby-forum.com/u/aidy)\
**Post date:** [July 23, 2008, 7:20pm UTC](https://www.ruby-forum.com/t/multiple-constructors/143910/1 "2008-07-23T19:20:10Z")

</div>

Hi,

Is it possible to use multiple constructors in Ruby?

class SQLServerConnection

def initialize(server\_name, db)

end

def initialize(server\_name, db, user\_name, password)

end  
end

Thank You

Aidy

---

<div class="post-metadata">

**Author:** ![aidy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/eada6e/32.png) [@aidy](https://www.ruby-forum.com/u/aidy)\
**Post date:** [July 23, 2008, 7:33pm UTC](https://www.ruby-forum.com/t/multiple-constructors/143910/2 "2008-07-23T19:33:05Z")

</div>

On Wed, Jul 23, 2008 at 12:09 PM, aidy [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> def initialize(server\_name, db, user\_name, password)
> 
> end  
> end

You can send your initialize an array or hash, or you can do

class C  
def initialize( a, b, \*c )  
#verify and do stuff  
end  
end

hth,  
Todd

---

<div class="post-metadata">

**Author:** ![aidy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/eada6e/32.png) [@aidy](https://www.ruby-forum.com/u/aidy)\
**Post date:** [July 23, 2008, 7:34pm UTC](https://www.ruby-forum.com/t/multiple-constructors/143910/3 "2008-07-23T19:34:16Z")

</div>

On Wed, Jul 23, 2008 at 7:09 PM, aidy [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> def initialize(server\_name, db, user\_name, password)
> 
> end  
> end
> 
> Thank You
> 
> Aidy

First of all initialize is not a constructor but an initializer,  
behind the scenes the following happenes

class Object  
def self.new \*args, &blk  
o = allocate  
o.send :initialize, \*args, &blk # this because #initialize is  
_always_ private  
o  
end  
end

As you can see #initialize is an instance method and #new is a  
singleton (or class) method.

Secondly, and that holds for all methods, #initialize is only an  
example, you cannot “overload” method definitions but simulate that  
behavior, in your case I would do the following

class SQLServerConnection

private

def initialize \*args  
case args.size  
when 2  
init\_name\_db \*args  
when 4  
init\_user\_pw \*args  
else  
error  
end  
end

def init\_name\_db server\_name, db  
…  
end

def init\_user\_pwd server\_name, db, user, pwd  
…  
end  
end

HTH  
Robert

–  
[http://ruby-smalltalk.blogspot.com/](http://ruby-smalltalk.blogspot.com/)

There’s no one thing that’s true. It’s all true.

---

<div class="post-metadata">

**Author:** ![aidy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/eada6e/32.png) [@aidy](https://www.ruby-forum.com/u/aidy)\
**Post date:** [July 23, 2008, 7:34pm UTC](https://www.ruby-forum.com/t/multiple-constructors/143910/4 "2008-07-23T19:34:51Z")

</div>

On Jul 23, 2008, at 1:09 PM, aidy wrote:

> def initialize(server\_name, db, user\_name, password)
> 
> end  
> end
> 
> Thank You
> 
> Aidy

No, but you can have optional parameters by giving a default value or  
taking a hash.

def initialize(server\_name, db, user\_name=nil, password=nil)  
if user\_name && password  
# connect with credentials  
else  
# connect anonymously  
end  
end

Called like: SQLServerConnection.new(“myserver”, “development”,  
‘aidy’, ‘shh\_secret’)

-or-

def initialize(server\_name, db, options={})  
if options.has\_key?(:user) && options.has\_key?(:password)  
# connect with credentials  
else  
# connect anonymously  
end  
end

and called like: SQLServerConnection.new(“myserver”,  
“development”, :user =\> ‘aidy’, :password =\> ‘shh\_secret’)

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![aidy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/eada6e/32.png) [@aidy](https://www.ruby-forum.com/u/aidy)\
**Post date:** [July 23, 2008, 7:36pm UTC](https://www.ruby-forum.com/t/multiple-constructors/143910/5 "2008-07-23T19:36:01Z")

</div>

aidy wrote:

> Is it possible to use multiple constructors in Ruby?

No.

> class SQLServerConnection  
> def initialize(server\_name, db)
> 
> end  
> def initialize(server\_name, db, user\_name, password)
> 
> end  
> end

def initialize(server\_name, db, user\_name=nil, password=nil)

end

HTH,  
Sebastian

---

<div class="post-metadata">

**Author:** ![aidy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/eada6e/32.png) [@aidy](https://www.ruby-forum.com/u/aidy)\
**Post date:** [July 24, 2008, 10:24pm UTC](https://www.ruby-forum.com/t/multiple-constructors/143910/6 "2008-07-24T22:24:50Z")

</div>

I prefer to use different names for “different constructors”.  
Most of the time, I feel that overloading can complicate things,  
more-so in a dynamic language.

Just make them class methods:

Foo.from\_x()  
Foo.with\_size()

Etc, etc.

HTH,  
Paul
