From: Victor Shepelev Date: 2006-06-06T22:49:55+09:00 Subject: Re: Call rb_instance_eval from C code From: why the lucky stiff [mailto:ruby-talk@whytheluckystiff.net] Sent: Monday, June 05, 2006 11:40 PM > On Tue, Jun 06, 2006 at 04:43:10AM +0900, Mauricio Fernandez wrote: > > On Tue, Jun 06, 2006 at 04:12:14AM +0900, why the lucky stiff wrote: > > > rb_funcall(obj, rb_intern("instance_eval"), 1, proc); > > > > I don't think that will work: proc is not a String but a Proc > (TypeError). > > > > See [ruby-talk:180647] and [ruby-talk:188958]. > > Ohhhh. I guess define_method can take a proc, though. Back to ye olde > cloaker. > Based on Guy's example: > > wagon$ cat a.c > #include > > static VALUE > rb_proc_bind(VALUE obj, VALUE a) > { > VALUE meth, meta; > VALUE cloaker = ID2SYM(rb_intern("__cloaker__")); > meta = rb_singleton_class(obj); > rb_funcall(meta, rb_intern("define_method"), 2, cloaker, a); > meth = rb_funcall(meta, rb_intern("instance_method"), 1, cloaker); > rb_funcall(meta, rb_intern("remove_method"), 1, cloaker); > return rb_funcall(meth, rb_intern("bind"), 1, obj); > } > > VALUE > obj_eval_it(int argc, VALUE *argv, VALUE self) > { > VALUE meth, proc; > rb_scan_args(argc, argv, "0&", &proc); > > /* !^! here is your rb_instance_eval replacement, victor. !^! */ > meth = rb_proc_bind(self, proc); > return rb_funcall(meth, rb_intern("call"), 0); > } > > void Init_a() > { > VALUE obj = rb_funcall(rb_cObject, rb_intern("new"), 0); > rb_define_method(rb_cObject, "eval_it", obj_eval_it, -1); > rb_const_set(rb_cObject, rb_intern("OBJ"), obj); > } > > wagon$ ruby -ra -e 'p Object::OBJ; Object::OBJ.eval_it { @b = 12 }; p > Object::OBJ' > # > # > > But now I really don't understand specific_eval(). Thanks why! Would try this. Victor.