[#1816] Ruby 1.5.3 under Tru64 (Alpha)? — Clemens Hintze <clemens.hintze@...>

Hi all,

17 messages 2000/03/14

[#1989] English Ruby/Gtk Tutorial? — schneik@...

18 messages 2000/03/17

[#2241] setter() for local variables — ts <decoux@...>

18 messages 2000/03/29

[ruby-talk:01919] Re: object creation

From: Clemens Hintze <c.hintze@...>
Date: 2000-03-16 18:22:22 UTC
List: ruby-talk #1919
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

In This Thread