From: Joakim Andersson Date: 2002-02-18T03:32:52+09:00 Subject: Problems with Ruby in C Hi, I'm having a problem with getting instance_eval to do what I want it to do when I'm calling ruby from C. I've created a small ruby program and my c version of the same, both are included below, which illustrate the problem. The Ruby program does what is expected but the C program doesn't, so I'm hoping that some kind soul could tell me what I'm doing wrong. This is the Ruby program I'm trying to do in C: class Context def execute(str) instance_eval(str) end end a = Context.new() a.execute("a = 5 ; puts a") b = Context.new() b.execute("puts a") When running this program I get: 5 test5.rb:4:in `instance_eval': (eval):1:in `instance_eval': undefined local variable or method `a' for # (NameError) from test5.rb:4:in `instance_eval' from test5.rb:4:in `execute' from test5.rb:12 which is what I expected. My C version of the same program looks like this: #include "ruby.h" static VALUE cContext_execute(VALUE self, VALUE str) { return rb_funcall(self, rb_intern("instance_eval"), 1, str); } int main(void) { VALUE cContext, a, b; ruby_init(); cContext = rb_define_class("Context", rb_cObject); rb_define_method(cContext, "execute", cContext_execute, 1); a = rb_class_new_instance(0, NULL, cContext); rb_funcall(a, rb_intern("execute"), 1, rb_str_new2("a = 5 ; puts a")); b = rb_class_new_instance(0, NULL, cContext); rb_funcall(b, rb_intern("execute"), 1, rb_str_new2("puts a")); return 0; } But then running this program I get: 5 5 Thanks and regards, Joakim -- Joakim Andersson ; tyrak@borgship.net ; http://borgship.net/~tyrak/