How to create a ruby object via the c-api?

Hello,

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?
or a custom class object?

Best Regards,
Matthias

···

Do You Yahoo!?
Yahoo! Finance - Get real-time stock quotes
http://finance.yahoo.com

Hi,

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().
Ex.
const char *src = “^foo$”;
rb_reg_new(src, strlen(src), RE_OPTION_IGNORECASE)

or a custom class object?

rb_class_new_instance().

···

At Fri, 30 Aug 2002 02:29:51 +0900, Matthias Veit wrote:


Nobu Nakada

Hi,

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:

Regexp.new ('myPattern')
---> rb_funcall (rb_cRegexp, rb_intern ("new"), 1, rb_str_new2

(“myPattern”));

(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
for string.)

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
code.)

Opinion, anyone?

Regards,

Bill

···

============================================================================
nobu.nokada@softhome.net wrote:

Hi,

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().
Ex.
const char *src = “^foo$”;
rb_reg_new(src, strlen(src), RE_OPTION_IGNORECASE)

or a custom class object?

rb_class_new_instance().


Nobu Nakada

Thanks for all ideas and suggestions - I don’t really thought about maintenance,
but you are right, that could be problematic.

Best Regards,
Matthias

···

WDT == William Djaja Tjokroaminata billtj@y.glue.umd.edu wrote:

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
code.)


Do You Yahoo!?
Yahoo! Finance - Get real-time stock quotes
http://finance.yahoo.com