Johan Persson wrote:
Or at least, is there any documentation aviable on the subject?
Hi Johan!
I looked through a bunch of examples in the RAA, and here
is what I hacked together, and it seems to work… I’m just a newbie,
so take it all with a grain of salt…
– Glenn
···
=======================================================================
#include <ruby.h>
#include <stdio.h>
static VALUE defout_write(VALUE self, VALUE str)
{
str = rb_obj_as_string(str);
fprintf(stderr, “In defout_write: len=%d, str=‘%s’\n”, RSTRING(str)->len, RSTRING(str)->ptr);
rb_str_cat(self, RSTRING(str)->ptr, RSTRING(str)->len);
return Qnil;
}
static VALUE defout_cancel(VALUE self)
{
fprintf(stderr, “In defout_cancel\n”);
if (RSTRING(self)->len == 0) return Qnil;
RSTRING(self)->len = 0;
RSTRING(self)->ptr[0] = ‘\0’;
return Qnil;
}
static void init()
{
ruby_init();
#if RUBY_VERSION_CODE >= 160
ruby_init_loadpath();
#else
#if RUBY_VERSION_CODE >= 145
rb_ary_push(rb_load_path, rb_str_new2(“.”));
#endif
#endif
rb_defout = rb_str_new(“”, 0);
rb_io_binmode(rb_stdout); /* for mswin32 /
rb_define_singleton_method(rb_defout, “write”, (VALUE()(ANYARGS))&defout_write, 1);
rb_define_singleton_method(rb_defout, “cancel”, (VALUE(*)(ANYARGS))&defout_cancel, 0);
}
static void proc_args(int argc, char **argv)
{
ruby_script(argv[0]);
// Other stuff here for initializing FLTK…
}
int main(int argc, char **argv)
{
// fprintf(stderr, “Calling init…\n”);
init();
// fprintf(stderr, “Calling proc_args…\n”);
proc_args(argc, argv);
// fprintf(stderr, “Calling run…\n”);
Fl::run(); // This is my FLTK GUI… you might not have this,
// but you will need some kind of event loop or something.
// Note that my GUI has callbacks that call ‘ruby_eval’, below…
return 0;
}
VALUE ruby_eval(const char* str)
{
int state = 0;
fprintf(stderr, “Calling ruby: ‘%s’\n”, str);
VALUE val = rb_eval_string_protect(str, &state);
if (state == Qundef) {
fprintf(stderr, “ERROR: Undefined statement: ‘%s’\n”, str);
} else {
fprintf(stderr, “State=%d, return VALUE=%08lX\n”, state, val);
}
return val;
}