Paul E.C. Melis wrote: >Hello, >I was wondering if it is wise/common practice/allowed to have multiple >constructors for a class defined in a C extension. >Specifically, I'm currently doing the following in my extension: > >rb_define_singleton_method(c..., "new", t_new, 1); >rb_define_singleton_method(c..., "new_from_file", t_new_from_file, 1); > >With both calls a structure is allocated and wrapped with >Data_Wrap_Struct(), followed by rb_obj_call_init() on the created object. > >The idea of course is to be able to say >obj1 = MyClass.new(...) >and >obj2 = MyClass.new_from_file('some_file') >in my script. > >The defined initialize method in the extension actually does nothing, so I >would rather leave it out. I don't see any reason myself why object >construction and initialization can't be done completely by the C functions >implementing new and new_from_file. Is there anything special about the >"new" singleton method other than that it implicitly calls "initialize"? > This brings another question, why does ruby use as default an instance method (initialize) for doing what must be a class method (new). I think that the current approach, does not follow the least surprise principle. You can of course do: class Test def Test.new_from_file('someFile') aTempVariable=Test.new # because it seems to be the only contructor allowed # ... whatever other things this routine does return aTempVariable end end This way you can then aTestVar = Test.new_from_file('someFile') But it's unnatural and strange (at least for me ...) Enric