Multiple constructor in Ruby

Discussion in 'Ruby' started by Arturo Bonechi, Nov 18, 2010.

  1. Hello!

    I have a question about the constructor in Ruby.

    Here is an exemple class :

    class Test
    def initialize param
    puts param
    end
    def initialize param1, param2
    puts "#{param1} #{param2}"
    end
    end

    If I call the second constructor, everything works fine, whereas I got
    an 'ArgumentError: wrong number of arguments (1 for 2)' with the first.

    So, has the second constructor overriden the first one? Does that mean
    you have to implement 'initialize' only once in a class?

    Thank you!

    --
    Posted via http://www.ruby-forum.com/.
     
    Arturo Bonechi, Nov 18, 2010
    #1
    1. Advertising

  2. Arturo Bonechi

    Jeremy Bopp Guest

    On 11/18/2010 9:41 AM, Arturo Bonechi wrote:
    > Hello!
    >
    > I have a question about the constructor in Ruby.
    >
    > Here is an exemple class :
    >
    > class Test
    > def initialize param
    > puts param
    > end
    > def initialize param1, param2
    > puts "#{param1} #{param2}"
    > end
    > end
    >
    > If I call the second constructor, everything works fine, whereas I got
    > an 'ArgumentError: wrong number of arguments (1 for 2)' with the first.
    >
    > So, has the second constructor overriden the first one? Does that mean
    > you have to implement 'initialize' only once in a class?


    This is true for any method definition in Ruby, not just constructors.
    There is no facility to select methods based on method signature, so
    Ruby sees defining a method with the same name as an existing one as
    replacing or overriding the original.

    -Jeremy
     
    Jeremy Bopp, Nov 18, 2010
    #2
    1. Advertising

  3. [Note: parts of this message were removed to make it a legal post.]

    On Thu, Nov 18, 2010 at 10:41 AM, Arturo Bonechi
    <>wrote:

    > Hello!
    >
    > I have a question about the constructor in Ruby.
    >
    > Here is an exemple class :
    >
    > class Test
    > def initialize param
    > puts param
    > end
    > def initialize param1, param2
    > puts "#{param1} #{param2}"
    > end
    > end
    >
    > If I call the second constructor, everything works fine, whereas I got
    > an 'ArgumentError: wrong number of arguments (1 for 2)' with the first.
    >
    > So, has the second constructor overriden the first one? Does that mean
    > you have to implement 'initialize' only once in a class?
    >
    > Thank you!
    >
    > --
    > Posted via http://www.ruby-forum.com/.
    >
    >

    The answer to both of your questions is yes. Well, you *can* implement it
    twice, but, as in your code, the first one is irrelevant. Remember that
    while defining a class, all you're doing is executing code. Anyway, things
    like default values and the hash options idiom make method overloading quite
    unnecessary.
     
    Andrew Wagner, Nov 18, 2010
    #3
  4. Ok, thanks to both of you for those quick replies!

    That explains a lot of things and solves my problem.

    Thank you again!

    --
    Posted via http://www.ruby-forum.com/.
     
    Arturo Bonechi, Nov 18, 2010
    #4
    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. Giulio
    Replies:
    9
    Views:
    1,046
    Patrick Kowalzick
    Jun 25, 2003
  2. Brett Irving
    Replies:
    3
    Views:
    3,337
    John Harrison
    Jun 29, 2003
  3. lallous
    Replies:
    5
    Views:
    8,823
    David Harmon
    Jan 23, 2004
  4. Aire
    Replies:
    3
    Views:
    467
    Mike Wahler
    Jan 25, 2004
  5. Generic Usenet Account
    Replies:
    10
    Views:
    2,244
Loading...

Share This Page