C
Charles Mills
I have a question about what is the best way to call Ruby methods which
are implemented in C from C.
The two basic ways I see are :
1) using one of the rb_funcall[0-9] C functions.
2) calling the actual C function.
For example if I am pushing objects onto an array in my C code I could do
this:
VALUE arr = rb_ary_new2(some_num);
VALUE some_obj = ...;
rb_ary_push(arr, some_obj);
or this:
VALUE arr = rb_ary_new2(some_num);
VALUE some_obj = ...;
rb_funcall(arr, rb_intern("push"), 1, some_obj);
So my main question is:
should you only call the C function defined in "ruby.h" directly (not
using rb_funcall) in you extensions or is it OK to call the C functions
defined in "intern.h" directly as well?
thoughts:
I assume "ruby.h" and "intern.h" are seperated for a reason and you should
limit you C code to using stuff from "ruby.h" only, ie don't include
"intern.h" in your extensions. One reason being that if you don't use
rb_funcall or a similar function no OO dispatch will take place. So if
someone overrides "push" and you call push using rb_ary_push you won't be
calling the version of "push" you should be.
Any opinions on this?
-Charlie
are implemented in C from C.
The two basic ways I see are :
1) using one of the rb_funcall[0-9] C functions.
2) calling the actual C function.
For example if I am pushing objects onto an array in my C code I could do
this:
VALUE arr = rb_ary_new2(some_num);
VALUE some_obj = ...;
rb_ary_push(arr, some_obj);
or this:
VALUE arr = rb_ary_new2(some_num);
VALUE some_obj = ...;
rb_funcall(arr, rb_intern("push"), 1, some_obj);
So my main question is:
should you only call the C function defined in "ruby.h" directly (not
using rb_funcall) in you extensions or is it OK to call the C functions
defined in "intern.h" directly as well?
thoughts:
I assume "ruby.h" and "intern.h" are seperated for a reason and you should
limit you C code to using stuff from "ruby.h" only, ie don't include
"intern.h" in your extensions. One reason being that if you don't use
rb_funcall or a similar function no OO dispatch will take place. So if
someone overrides "push" and you call push using rb_ary_push you won't be
calling the version of "push" you should be.
Any opinions on this?
-Charlie