Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Ruby (http://www.velocityreviews.com/forums/f66-ruby.html)
-   -   Multiple constructor in Ruby (http://www.velocityreviews.com/forums/t865294-multiple-constructor-in-ruby.html)

Arturo Bonechi 11-18-2010 03:41 PM

Multiple constructor in Ruby
 
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/.


Jeremy Bopp 11-18-2010 03:46 PM

Re: Multiple constructor in Ruby
 
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


Andrew Wagner 11-18-2010 03:49 PM

Re: Multiple constructor in Ruby
 
[Note: parts of this message were removed to make it a legal post.]

On Thu, Nov 18, 2010 at 10:41 AM, Arturo Bonechi
<arturo.bonechi@gmail.com>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.


Arturo Bonechi 11-18-2010 04:07 PM

Re: Multiple constructor in Ruby
 
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/.



All times are GMT. The time now is 05:04 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.