rb_iterate

忘れがちなのでメモ。

#include "ruby.h"

static VALUE main_proc(VALUE arg) {
  VALUE each = arg;
  return rb_funcall(each, rb_intern("call"), 0);
}

static VALUE block(VALUE call_arg, VALUE block_arg, VALUE self) {
  rb_p(call_arg);
  rb_p(block_arg);
  return Qnil;
}

static VALUE hello(VALUE self) {
  VALUE ary, each;
  ary = rb_ary_new3(3, Qnil, Qtrue, Qfalse);
  each = rb_obj_method(ary, ID2SYM(rb_intern("each"))); // Get bound method.

  rb_iterate(/* Main proc */ main_proc, /* Main proc arg */ each,
    /* Block */ block, /* Block arg */ rb_str_new2("block arg"));

  return Qnil;
}

__declspec(dllexport)
void Init_hello() {
  VALUE m;

  m = rb_define_module("Hello");
  rb_define_module_function(m, "hello", hello, 0);
}
require 'hello'
include Hello
hello


nil
"block arg"
true
"block arg"
false
"block arg"
続行するには何かキーを押してください . . .