--001636c5a81cd5cb2f049555bbd9 Content-Type: text/plain; charset=UTF-8 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. --001636c5a81cd5cb2f049555bbd9--