I think what Nobu wrote is the most efficient way to create the
objects. However, if you just want to do simple translation from Ruby to
C, you can do something like the following:
---> rb_funcall (rb_cRegexp, rb_intern ("new"), 1, rb_str_new2
(Just be aware that in the “myPattern” above the C language will perform
character substitution; the problem is we cannot use single quotes in C
In fact, I have to make this choice all the time: should I use the most
direct call of the C API, or should I use the Ruby-to-C translation
format? When I asked Matz some time ago whether we have an official set
of Ruby C API’s, the answer was “yes and no”. I think in a future release
of Ruby (Rite?) some of the C API’s may change, whereas the Ruby
"language" will stay approximately the same for backward compatibility.
So I think, using simple translation will protect more against future Ruby
release, while using the most direct function call will result in a more
efficient C code. The way I choose is usually if a piece of code is
really critical (called very often), I use the most direct
function; otherwise I just do simple translation.
Regarding documentation, I also only accessed the “Extending
Ruby” chapter, the README.EXT file, and of course, this discussion
group. (And sooner or later, I think you will start reading the source
At Fri, 30 Aug 2002 02:29:51 +0900, > Matthias Veit wrote:
how is it possible to create a certain ruby object via the c-api?
For some base types there are methods in ruby.h ala rb_ary_new etc.
But how to create a Regexp object?
You can use rb_reg_new().
const char *src = “^foo$”;
rb_reg_new(src, strlen(src), RE_OPTION_IGNORECASE)
or a custom class object?