From: Rob Biedenharn Date: 2008-07-24T02:26:12+09:00 Subject: Re: multiple constructors? On Jul 23, 2008, at 1:09 PM, aidy wrote: > 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 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 Biedenharn http://agileconsultingllc.com Rob@AgileConsultingLLC.com