Hi,
Thanks for the help. So again and again, the key is apparently
“rb_iterate()”. Am I correct if I assume that we cannot create a
“physical” Ruby block in C (something which is of class Proc, which can
be “call”-ed)?
In 1.7, you can use rb_proc_new(), however, in 1.6 you need to
- create temporary object,
- define singleton method of the object with the function,
- get the Method object, and
- convert it to Proc.
See nodeDump for detail.
(Yesterday when I browsed the Ruby classes, there is no such thing as
“proc.c”, even though Proc is one of the built-in classes. So I assume
that Proc and Ruby block are “special”, which belong only to the Ruby
parser/interpreter, but not accessible via the C API’s. Now I see where
Joel VanderWerf was going when he answered how to convert a Proc into a
block in C.)
It’s defined in eval.c. Well, you can call it “special” in the
sense that it needs to access interpreter internals.
Is there an easier or standard way of accessing the “logically enclosing
local” variable in a “logical Ruby block” in C, such as for the
“nextState” variable in the code above?
Pass struct address casting to VALUE.
untested code.
struct arg {
VALUE trans, nextState;
};
static VALUE
statements(VALUE key, struct arg *arg)
{
if (rb_funcall(arg->trans, rb_intern("[]"), 1, key) == Qtrue) {
if (NIL_P(arg->nextState))
arg->nextState = key;
else
rb_raise(rb_eRuntimeError, "multiple transitions defined");
}
return Qnil;
}
static VALUE
call_func(VALUE obj)
{
return rb_funcall(obj, rb_intern("each_key"), 0);
}
struct arg arg;
arg.trans = trans;
arg.nextState = Qnil;
rb_iterate(call_func, trans, statement, (VALUE)&arg);
···
At Thu, 5 Sep 2002 22:47:41 +0900, William Djaja Tjokroaminata wrote:
At Fri, 6 Sep 2002 00:27:57 +0900, William Djaja Tjokroaminata wrote:
–
Nobu Nakada