Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > multiple constructors?

Reply
Thread Tools

multiple constructors?

 
 
aidy
Guest
Posts: n/a
 
      07-23-2008
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
 
Reply With Quote
 
 
 
 
Todd Benson
Guest
Posts: n/a
 
      07-23-2008
On Wed, Jul 23, 2008 at 12:09 PM, aidy <(E-Mail Removed)> 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


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

 
Reply With Quote
 
 
 
 
Robert Dober
Guest
Posts: n/a
 
      07-23-2008
On Wed, Jul 23, 2008 at 7:09 PM, aidy <(E-Mail Removed)> 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
>
>


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/

There's no one thing that's true. It's all true.
--
Ernest Hemingway

 
Reply With Quote
 
Rob Biedenharn
Guest
Posts: n/a
 
      07-23-2008
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?(assword)
# connect with credentials
else
# connect anonymously
end
end

and called like: SQLServerConnection.new("myserver",
"development", :user => 'aidy', assword => 'shh_secret')

-Rob

Rob Biedenharn http://agileconsultingllc.com
http://www.velocityreviews.com/forums/(E-Mail Removed)



 
Reply With Quote
 
Sebastian Hungerecker
Guest
Posts: n/a
 
      07-23-2008
aidy wrote:
> Is it possible to use multiple constructors in Ruby?


No.


> class SQLServerConnection
> =A0 def initialize(server_name, db)
>
> =A0 end
> =A0 def initialize(server_name, db, user_name, password)
>
> =A0 end
> end


def initialize(server_name, db, user_name=3Dnil, password=3Dnil)

end


HTH,
Sebastian
=2D-=20
Jabber: (E-Mail Removed)
ICQ: 205544826

 
Reply With Quote
 
Paul Stickney
Guest
Posts: n/a
 
      07-24-2008
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Multiple ISPs and Multiple IP Ranges from Each ISP Chennak Cisco 10 06-08-2005 09:29 PM
IP Address Scheme for Multiple DMZs on Multiple PIXs Scotchy Cisco 2 10-07-2004 03:35 PM
Preventing multiple form submissions (multiple postbacks) Diane Selby ASP .Net 4 06-25-2004 09:50 PM
multiple dropdown lists = multiple data readers??? Andrew Kidd ASP .Net 3 04-22-2004 03:23 PM
Re: One solution containing multiple projects and multiple hosts Stephen Miller ASP .Net 0 06-24-2003 12:55 PM



Advertisments