Andrew Hunt writes: > >| NEWOBJ(obj, c-type) -- allocate memory for an object > >| OBJSETUP(obj, class, T_class); -- set appropriate flags > >| rb_obj_call_init(obj, argc, argv); -- call initialize > > > >Yes. But I think it's better for T_DATA object to use > >Data_Make_Struct/Data_Wrap_Struct, as Clemens stated. > > Agreed, but here's my real question: suppose you have a non-built- > in class such as MD5, which does not expose a C-level constructor > (something like rb_str_new, rb_time_new, etc). What is the > preferred way to create a new MD5 object from C code? Hmmmm! I would say that this is impossible, at a first glance. This MD5 class has to implement a C function that would be connected to MD5::new at Ruby level. So you would have to search something like (example taken from ext/gdbm/gdbm.c): rb_define_singleton_method(cGDBM, "new", fgdbm_s_open, -1); Here you can see, that whenever I invoke GDBM::new, the C function fgdbm_s_open will be called instead! So I could easily call: VALUE filename = rb_str_new2("mydata.gdbm"); VALUE args[] = { filename }; VALUE gdbm_obj = fgdbm_s_open(1, args); I admit, it looks a little bit complicate. That is the reason, extensions should have a method like rb_str_new2 (that I would call an C constructor) too, if they are intended to be used on C level. This method should be available all the time. Or Ruby would not be able to create instances of it. But wait ... I have forgot ... you could, of course, do not implement a 'new' method and let the initialize function do the memory allocation. But here too, you would to have to look, what C function is behind 'initialize', and then call the C constructor of the parent class, and pass this instance to the 'initialize' method. But I have not met such constellation yet! > Thanks, You are welcome :-) > /\ndy -- Clemens Hintze mailto: c.hintze / gmx.net