From: matz@... (Yukihiro Matsumoto) Date: 2001-02-04T03:30:40+09:00 Subject: [ruby-talk:10314] Re: More C API examples? Hi, In message "[ruby-talk:10311] More C API examples?" on 01/02/04, Amos writes: |I was wondering if someone has examples of defining and then |instantiating class objects in the C API. While I see stuff like |NEWOBJ and rb_class_new in the Ruby source code, it's not mentioned |specifically in the README.EXT nor in the "pickaxe" book, so I was |hoping to find more examples of how this should be done. Don't use NEWOBJ() in your extension, since it requires own unique T_XXXX identity number for each object type, which, I think, is special privilege of the interpreter itself. That is the reason why it is not explained in README.EXT nor "pickaxe" book. I recommend you to use Wrap_Data_Struct() and the like. |Don't know, so thought I'd give it a try. At least it would be a |learning experience. To satisfy your curiosity, obey following steps to add new type into the interpreter: (1) define new and unique T_XXX value in ruby.h. it must be less than 0x40 and strictly UNIQUE from other type (T_XXX) values. (2) create object using NEWOBJ(varname, struct), a created object will be assigned to varname, whose type is a pointer to struct. varname is declared internally in the macro, so that it must be a first statement (after declarations) in the function/block. (3) set up type information of a new object using OBJSETUP(). DO NOT FORGET THIS. if you forget, the program might be crash. (4) modify gc.c. add T_XXX entry to rb_gc_mark() and obj_free(). DO NOT FORGET THIS (I'm yelling?). matz.