multiple constructors?

Discussion in 'Ruby' started by aidy, Jul 23, 2008.

  1. aidy

    aidy Guest

    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
     
    aidy, Jul 23, 2008
    #1
    1. Advertising

  2. aidy

    Todd Benson Guest

    On Wed, Jul 23, 2008 at 12: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


    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
     
    Todd Benson, Jul 23, 2008
    #2
    1. Advertising

  3. aidy

    Robert Dober Guest

    On Wed, Jul 23, 2008 at 7: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
    >
    >


    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
     
    Robert Dober, Jul 23, 2008
    #3
  4. 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 Biedenharn, Jul 23, 2008
    #4
  5. 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:
    ICQ: 205544826
     
    Sebastian Hungerecker, Jul 23, 2008
    #5
  6. 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
     
    Paul Stickney, Jul 24, 2008
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dave Rudolf
    Replies:
    12
    Views:
    8,331
    Martijn Lievaart
    Feb 6, 2004
  2. Jeremy Smith
    Replies:
    2
    Views:
    603
    Jeremy Smith
    Aug 3, 2006
  3. Jess
    Replies:
    5
    Views:
    615
    Ron Natalie
    Jun 7, 2007
  4. Peng Yu
    Replies:
    5
    Views:
    403
    Juha Nieminen
    Sep 19, 2008
  5. srp113
    Replies:
    3
    Views:
    482
Loading...

Share This Page