From: Dennis Schridde Date: 2005-09-11T00:05:42+09:00 Subject: Re: Embedding Ruby into C --nextPart3192210.jG0MBPicdI Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable Content-Disposition: inline Am Freitag, 9. September 2005 20:44 schrieb Bill Kelly: > 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 =3D 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 =3D=3D 0) { > int state =3D 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. Thanks! Now there are only two questions left: 1st -- How do I call the Ruby method from C? I know that I must call it by ID and give also a reciever... But how do I g= et=20 them? Or is there another method, without ID and reciever needed? 2nd -- How do I callback? Thanks, Dennis --nextPart3192210.jG0MBPicdI Content-Type: application/pgp-signature -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.9.18 (GNU/Linux) iD8DBQBDIvZCNNHWXccyBJIRAvoCAKDaJYJXkTeO6EHzGbvpL+8ux50XgQCeKkLq KV4Nud2cPpEOGeOO9ScJFUY= =9067 -----END PGP SIGNATURE----- --nextPart3192210.jG0MBPicdI--