thanks dave, for the pointer.
now I got the following code works (but there is some problem):
static VALUE eval_failed()
{
printf("eval failed\n");
return Qnil;
}
static VALUE my_each(VALUE obj)
{
return rb_funcall(obj, rb_intern("each"), 0);
}
static VALUE my_iterator(VALUE obj)
{
int error;
VALUE result;
result = rb_protect(my_each, obj, &error);
return error ? Qnil : result;
}
static VALUE my_iterator_block(VALUE res)
{
printf("args: %s\n", STR2CSTR(rb_inspect(res)));
return Qnil;
}
int main(void) {
char *silly = "def foo; end";
ruby_init();
ruby_init_loadpath();
/* def foo */
rb_rescue(rb_eval_string, (VALUE)silly, eval_failed, 0);
/* Object.private_methods */
{
VALUE obj = rb_funcall(rb_cObject, rb_intern("private_methods"), 0);
if (TYPE(obj) == T_ARRAY) {
printf("Array!\n");
/* obj.each {|v| pp v } */
rb_iterate(
my_iterator,
obj,
my_iterator_block,
0
);
} else {
printf("Not array! Can't proceed\n");
}
}
ruby_finalize();
exit(0);
}
__END__
but "foo" is not there! why is it different from:
def foo
end
Object.private_methods.each do |x|
puts x
end
puts "===================="
Object.private_methods.select {|x| x =~ /foo/ }.each do |x|
puts x
end
__END__
rgds,
Edwin.
···
--- Dave Thomas <dave@pragprog.com> wrote:
On Jan 9, 2008, at 4:17 AM, Edwin Pratomo wrote:
> in particular, what I would like to do is Object.private_methods, in
> C way.
To call a method that's available in RUby, just use rb_funcall
rb_funcall(receiver, mid, argc, arg, arg, ...)
The mid parameter is the ID of the method to invoke (nor its name) and
the arguments (if any) are VALUEs,
________________________________________________________
Bergabunglah dengan orang-orang yang berwawasan, di di bidang Anda! Kunjungi Yahoo! Answers saat ini juga di http://id.answers.yahoo.com/