From: Stefano Crocco Date: 2007-03-20T21:35:24+09:00 Subject: Re: does Ruby not support multiple "initialize" methods for a class??? Alle marted� 20 marzo 2007, Greg Hauptmann ha scritto: > I have a class that can be created from a couple of scenarios, and for each > the initial data to populate the class is different. I was going to create > two "initialize" methods with their own specific method signature to cover > off these two scenarios however I guess I will need to perhaps just create > two separate/normal methods for this. Perhaps then it would be like: > > a = Klass.new > a.create_for_scenario_one (...) You could just declare an initialize method which takes any number of arguments, then detect the scenario according to the passed argument. For example, assuming that in one case the first argument is a number and in the second is a string, you could do: class MyClass def initialize(*args) if args[0].is_a? Numeric #perform initialization in first case elsif args[0].is_a? String #perform initialization in second case else raise TypeError end end end I hope this helps Stefano