J
Joel VanderWerf
I'm stumped... how do I call instance_eval from a C extension?
I have an object and a proc:
VALUE obj, pr;
and I want to eval pr with obj as self.
Currently, I do something like this, which works, but it's inefficient:
rb_funcall(obj, rb_intern("insteval_proc"), 1, pr);
and I have the following defined in ruby:
class Object
def insteval_proc pr
instance_eval &pr
end
end
I'd rather avoid the extra method call.
It doesn't seem like I can use rb_obj_instance_eval(), because that
takes string arguments, not block arguments. Of course, if you have a
block accompanying the method call, it will instance_eval that, but I
don't know how to set that up in C. That's why I do "instance_eval &pr"
in my little hack.
And I can't see how to use rb_iterate(). This is what I tried:
static VALUE my_instance_eval(VALUE obj)
{
return rb_obj_instance_eval(0, 0, obj);
}
static VALUE call_block(VALUE arg1, VALUE block)
{
return rb_funcall(block, ID_call, 0);
}
//...
rb_iterate(my_instance_eval, obj, call_block, pr);
but I get "block not supplied (ArgumentError)", so I guess it's not
being propagted into the rb_funcall.
Is there a simple way to emulate "&block" in C?
Even better, is there a way to rebind the proc's self to my object, so I
only have to do it once, and can just call the block instead of using
instance eval?
I have an object and a proc:
VALUE obj, pr;
and I want to eval pr with obj as self.
Currently, I do something like this, which works, but it's inefficient:
rb_funcall(obj, rb_intern("insteval_proc"), 1, pr);
and I have the following defined in ruby:
class Object
def insteval_proc pr
instance_eval &pr
end
end
I'd rather avoid the extra method call.
It doesn't seem like I can use rb_obj_instance_eval(), because that
takes string arguments, not block arguments. Of course, if you have a
block accompanying the method call, it will instance_eval that, but I
don't know how to set that up in C. That's why I do "instance_eval &pr"
in my little hack.
And I can't see how to use rb_iterate(). This is what I tried:
static VALUE my_instance_eval(VALUE obj)
{
return rb_obj_instance_eval(0, 0, obj);
}
static VALUE call_block(VALUE arg1, VALUE block)
{
return rb_funcall(block, ID_call, 0);
}
//...
rb_iterate(my_instance_eval, obj, call_block, pr);
but I get "block not supplied (ArgumentError)", so I guess it's not
being propagted into the rb_funcall.
Is there a simple way to emulate "&block" in C?
Even better, is there a way to rebind the proc's self to my object, so I
only have to do it once, and can just call the block instead of using
instance eval?