Mix code of C and Ruby, need help pls

Hello,

I am trying to write some mix code of C and Ruby but I’m not able to
complete it and cannot find any documentation about it.

Let say I have a ruby script that looks like this:

Foo.rb
class Foo
attr_reader :val

def func
@val = 1;
end
end

I want to write the following Ruby code in C in order to access my foo
class.

obj = Foo.new
obj.func
print "Value: " + obj.val

So far, I have try those approaches in C:

rb_eval_string( “require “Foo.rb”” );
rb_eval_string( “obj = Foo” );
rb_eval_string( “obj.func” );

but I’m totally clueless how to do the last line. I tryed to find
something that look like this but with no success.

printf( “Value: %d”, rb_eval_string( “obj.val” ) );

I also have tryed something else but I guess I’m totaly wrong since it
doesn’t work at all:

VALUE obj;
rb_require( “Foo.rn” );
obj = rb_class_new( “Foo” );
rb_funcall( obj, rb_intern( “func” ), 0 );
printf( “Value: %d”, rb_iv_get( obj, “@val” ) );

Any help, or link to a documentation would be appreciated.

Thank you,
Ben.

Hi,

I also have tryed something else but I guess I’m totaly wrong since it
doesn’t work at all:

VALUE obj;
rb_require( “Foo.rn” );
rb
obj = rb_class_new( “Foo” );

rb_class_new(superclass) does create a new class inheriting
superclass.

obj = rb_class_new_instance(0, NULL, rb_const_get(rb_cObject, rb_intern(“Foo”)));

···

At Tue, 18 Feb 2003 07:41:37 +0900, Ben Thomas wrote:

rb_funcall( obj, rb_intern( “func” ), 0 );
printf( “Value: %d”, rb_iv_get( obj, “@val” ) );


Nobu Nakada