From: ts Date: 2002-04-23T02:12:30+09:00 Subject: Re: newbie embedding >>>>> "J" == Jamie Knight writes: J> class A J> def test() J> puts "rah" J> end J> end J> and the following c: J> #include "ruby.h" J> void main() J> { J> VALUE classy; J> ID function; J> ruby_init(); J> ruby_script("embedded"); J> rb_load_file("test.rb"); J> classy=rb_const_get(rb_cObject,rb_intern("A")); J> function=rb_intern("test"); J> rb_funcall(classy,function,0); J> } You have 2 errors : 1) rb_load_file() compile the file but don't execute the nodes 2) you are trying to call a *class* method (i.e. the receiver is classy, the class) and in test.rb you have defined an *instance* method Write it like this : pigeon% cat test.rb class A def test() puts "rah" end end pigeon% pigeon% cat aa.c #include "ruby.h" int main() { VALUE classy, obj; ID function; ruby_init(); ruby_script("embedded"); rb_load(rb_str_new2("test.rb"), 0); classy = rb_const_get(rb_cObject,rb_intern("A")); obj = rb_funcall(classy, rb_intern("new"), 0); function = rb_intern("test"); rb_funcall(obj,function,0); return 0; } pigeon% pigeon% aa rah pigeon% First you call A.new to have an object, then you call the instance method of this object. There is a problem with my script. If ruby don't find "test.rb" it will call rb_raise() and this will crash your program, because you have not protected the call to rb_load() You can use rb_protect() for this something like this : pigeon% cat aa.c #include "ruby.h" VALUE call_test() { VALUE classy, obj; ID function; rb_load(rb_str_new2("test.rb"), 0); classy = rb_const_get(rb_cObject,rb_intern("A")); obj = rb_funcall(classy, rb_intern("new"), 0); function = rb_intern("test"); rb_funcall(obj,function,0); return Qnil; } int main() { int error = 0; ruby_init(); ruby_script("embedded"); rb_protect(call_test, 0, &error); if (error) { VALUE mes = rb_inspect(rb_gv_get("$!")); fprintf(stderr, "ERROR : %s\n", RSTRING(mes)->ptr); return 1; } return 0; } pigeon% pigeon% rm test.rb pigeon% pigeon% aa ERROR : # pigeon% Guy Decoux