hi!
from a singleton method i want to call the
hidden class method. directly using the
ruby executable this works perfectly:
#v+
x = {}
def x.inspect
p "myInspect"
super
end
p x.inspect
#v-
output is:
"myInspect"
"{}"
....but when i try to perform this using the C API:
#v+
#include <stdio.h>
#include <ruby.h>
VALUE myInspect (VALUE self)
{
printf ("myInspect\n");
return rb_call_super(0,0);
}
int main()
{
VALUE x,y;
ruby_init();
x = rb_hash_new();
rb_define_singleton_method(x,"inspect",(VALUE (*)()) myInspect,0);
/* call now: */
y = rb_funcall(x,rb_intern("inspect"),0);
printf ("%s\n",STR2CSTR(y));
return 0;
}
#v-
it fails:
myInspect
ruby:0:in inspect': superclass method inspect' must be enabled by
rb_enable_super() (NameError)
from ruby:0
with 1.6.3 & 1.7.0 (2001-05-24)
so i tried to replace the 'call now' section:
#v+
/* call now: */
rb_gv_set("$x",x);
rb_eval_string("p $x");
#v-
....which results in the same error.
** but **:
#v+
/* call now: */
rb_gv_set("$x",x);
rb_eval_string("f=$x.method(:inspect); p f.call");
#v-
surprisingly works well!
output is:
myInspect
"{}"
(besides using a global variable is not applicable at all)
so, how can this be done properly?
ciao, andi