From: ara.t.howard@... Date: 2007-03-20T23:42:06+09:00 Subject: Re: does Ruby not support multiple "initialize" methods for a class??? On Tue, 20 Mar 2007, Greg Hauptmann wrote: > 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 can use class methods to return objects, as others have mentioned, but it also extremely easy to do your own function signature matching in ruby harp:~ > cat a.rb class C def initialize *argv, &block case argv when match(Fixnum) initialize_from_int argv[0] when match(Fixnum, Float) initialize_from_int_float argv[0], argv[1] else raise ArgumentError, argv.inspect end end def initialize_from_int int @int = int @float = 0.0 end def initialize_from_int_float int, float @int = int @float = float end class Pattern def initialize template @template = template end def === objects return false unless @template.size == objects.size @template.each_with_index do |pat, idx| return false unless pat === objects[idx] end return true end end def match *template Pattern.new template end end p C.new(42) p C.new(42,42.0) harp:~ > ruby a.rb # # however, function signature overloading is very confusing when mixed with default parameters. most people simply ignore this and live with the bugs in c++. regards. -a -- be kind whenever possible... it is always possible. - the dalai lama