Contents

組み込みRubyの標準出力

  • 組み込みRubyを使う場合、標準出力をどう処理すればいいか。
  • 何も処理しないとハングして停止する。
  • 場合によっては「Bad file descriptor (Errno::EBADF)」というエラーになる。

ruby_io_init

static VALUE vim_message(VALUE self, VALUE str)
{
    char *buff, *p;

    str = rb_obj_as_string(str);
    buff = ALLOCA_N(char, RSTRING(str)->len);
    strcpy(buff, RSTRING(str)->ptr);
    p = strchr(buff, '\n');
    if (p) *p = '\0';
    MSG(buff);
    return Qnil;
}

static VALUE f_p(int argc, VALUE *argv, VALUE self)
{
    int i;
    VALUE str = rb_str_new("", 0);

    for (i = 0; i < argc; i++) {
	if (i > 0) rb_str_cat(str, ", ", 2);
	rb_str_concat(str, rb_inspect(argv[i]));
    }
    MSG(RSTRING(str)->ptr);
    return Qnil;
}

static void ruby_io_init(void)
{
    extern VALUE rb_defout;

    rb_defout = rb_obj_alloc(rb_cObject);
    rb_define_singleton_method(rb_defout, "write", vim_message, 1);
    rb_define_global_function("p", f_p, -1);
}
Last modified: 2007-07-13