From: Simon Strandgaard <0bz63fz3m1qt3001@...> Date: 2003-05-15T00:03:31+09:00 Subject: Re: ruby_run() w/o exit? On Wed, 14 May 2003 15:31:14 +0000, ahoward wrote: > i peeked through ruby.h a little, and of course the pickaxe, but there does > not seem to be a function similar to ruby_run() which does not cause the > calling process to exit(). of course, one can use rb_eval_string(), etc... > > what would the best way to repeatedly call the interpreter? rb_funcall().. does *most* of what you want. You will have to wrap rb_funcall into a rb_protect() call in order to catch all exceptions. I you don't use rb_protect and an error occurs within your ruby code your application will be exit'ed. /* ------------------------------ wrap rb_funcall ------------------------------ */ struct Arguments { VALUE recv; ID id; int n; VALUE *argv; Arguments(VALUE recv, ID id, int n, VALUE *argv) : recv(recv), id(id), n(n), argv(argv) { } }; VALUE FuncallWrap(VALUE arg) { Arguments &a = *reinterpret_cast(arg); return rb_funcall2(a.recv, a.id, a.n, a.argv); } /* purpose: call a ruby function in a safe way. translate ruby errors into c++ exceptions. */ VALUE Funcall(VALUE recv, ID id, int n, ...) { VALUE *argv = 0; if(n > 0) { argv = ALLOCA_N(VALUE, n); va_list ar; va_start(ar, n); int i; for(i=0;i(&arg), &error); if(error) { //throw RubyError::Create("cannot invoke ruby-function"); throw; } return result; } VALUE Safe() { return Funcall( self, rb_intern("my_super_nice_test_function_in_ruby"), 1, INT2NUM(42) ); } See the turorial for furhter info: http://metaeditor.sourceforge.net/embed/index.html -- Simon Strandgaard