From: Bill Kelly Date: 2005-09-10T03:44:20+09:00 Subject: Re: Embedding Ruby into C From: "Dennis Schridde" > But I think this is again very complex and it uses C++, > what seems to make it relatively different from a plain C > approach. I haven't compiled the following - just cobbled it together from other code that does run. But maybe it will be of some help? #include "ruby.h" static VALUE mFoo; static VALUE foo_hello_world (VALUE self) { printf("Hello! I'm a C function, called from Ruby.\n"); return self; } int main (int argc, char **argv) { // Initialize Ruby....... #ifdef _WIN32 // ruby win32 init NtInitialize(&argc, &argv); #endif ruby_init(); ruby_script("embedded"); // define a new module "Foo" containing a method "hello_world" // which calls the C function foo_hello_world() mFoo = rb_define_module("Foo"); rb_define_module_function(mFoo, "hello_world", RUBY_METHOD_FUNC(foo_hello_world), 0); // load and execute a hypothetical "test.rb" which // can do whatever it wants - including call Foo.hello_world int status; rb_load_protect(rb_str_new2("test.rb"), 0, &status); if (status == 0) { int state = ruby_exec(); } // TODO: example calling arbitrary Ruby method from C return 0; } I didn't provide an example of calling an arbitrary Ruby method from C. I know it's easy, but I didn't have an example handy. Regards, Bill