2

I want to use rb_require with rb_protect, as in the following example:

int error;
rb_protect( (VALUE (*)(VALUE))rb_require, (VALUE) "./test", &error);

But when I compile it, I get this error:

passing argument 1 of ‘rb_protect’ from incompatible pointer type [enabled by default]
/usr/include/ruby-1.9.1/ruby/intern.h:357:7: note: expected ‘VALUE (*)(VALUE)’ but argument is of type ‘VALUE (*)(VALUE,  VALUE)’

After searching on Google to see how to use rb_require with rb_protect, I tried:

int error;
rb_protect( RUBY_METHOD_FUNC(rb_require), (VALUE) "./test", &error);

or

VALUE require_wrap(VALUE arg)
{
return rb_require("./test");
}
/*in main:*/
rb_protect( require_wrap, 0, & error);

But I always get the same error. This error doesn't stop the compilation but the binary segfault when I launch it whereas everything works without the rb_protect.

__Edit__

there was an error in my source file. In fact all the solutions I have tested works well:

int error;
rb_protect( (VALUE (*)(VALUE))rb_require, (VALUE) "./test", &error);

or

int error;
rb_protect( RUBY_METHOD_FUNC(rb_require), (VALUE) "./test", &error);

or

VALUE require_wrap(VALUE arg)
{
  return rb_require("./test");
}
/*in main:*/
rb_protect( require_wrap, 0, & error);

Thanks

cedlemo
  • 3,205
  • 3
  • 32
  • 50
  • I can't help you with this problem, but you might find this [list](http://stackoverflow.com/tags/ruby-c-api/info) of resources helpful. – user2398029 Mar 12 '12 at 13:57
  • thanks for this list but I have already read a lot of those documentation. I wrote a module, some class and method for testing purpose. But now I want to be able to handle errors in the ruby file that I load in the embeded interpreter. The first code come from an official documentation but it doesn't works. – cedlemo Mar 12 '12 at 18:55
  • And this will not work? `rb_protect( rb_require, rb_str_new2("./test"), &error);` – thomthom Mar 15 '12 at 10:47

1 Answers1

0

Solutions that works well:

int error;
rb_protect( (VALUE (*)(VALUE))rb_require, (VALUE) "./test", &error);

or

int error;
rb_protect( RUBY_METHOD_FUNC(rb_require), (VALUE) "./test", &error);

or

VALUE require_wrap(VALUE arg)
{
  return rb_require("./test");
}
/*in main:*/
rb_protect( require_wrap, 0, & error);
cedlemo
  • 3,205
  • 3
  • 32
  • 50