Embeding Ruby in C++ code

I have written the following code:

#include <ruby.h>

VALUE do_something(VALUE str)
{
printf(“executing function: do_something\n”);
char *s = RSTRING(str)->ptr;
printf(“argument: %s\n”, s);

 return rb_tainted_str_new2("OK");

}

int main(int argc, char* argv[])
{
//Way 1
ruby_init();

 //Here i register my global function
 rb_define_global_function("do_something", (VALUE

(*)(…))&do_something, 1);
rb_eval_string(“do_something “hi!”\n”);
return 0;
}

The command gets evaluated ok but something seems to be wrong with the
argument i get in the
do_something function. If i use the TYPE(str) macro i get as a result 2
which equals T_OBJECT
instead of T_STRING as i expected. What am i doing wrong?

Thank you in advance
Kostas

I have written the following code:
[…]
The command gets evaluated ok but something seems to be wrong with the
argument i get in the do_something function. If i use the TYPE(str)
macro i get as a result 2 which equals T_OBJECT instead of T_STRING as
i expected. What am i doing wrong?

Maybe you need to use the C linkage conventions, ie ‘extern “C”’.

···

On Sun, Aug 11, 2002 at 04:50:54AM +0900, Kostas Katsamakas wrote:


_ _

__ __ | | ___ _ __ ___ __ _ _ __
'_ \ / | __/ __| '_ _ \ / ` | ’ \
) | (| | |
__ \ | | | | | (| | | | |
.__/ _,
|_|/| || ||_,|| |_|
Running Debian GNU/Linux Sid (unstable)
batsman dot geo at yahoo dot com

Try to remove the color-problem by restarting your computer several times.
– Microsoft-Internet Explorer README.TXT

This should be:
VALUE do_something(VALUE /* self */, VALUE str)

All methods in ruby get called on an object, even global functions.
Their object is ‘main’.

Paul

···

On Sun, Aug 11, 2002 at 04:50:54AM +0900, Kostas Katsamakas wrote:

VALUE do_something(VALUE str)