LoginSignup
13
13

More than 5 years have passed since last update.

Cからmrubyのクラスメソッドを呼び出す方法

Last updated at Posted at 2014-01-05

Big Sky :: mruby でmrbgemsから別のmrbgemsのクラスを参照して継承する

上記サイトを参考にしながら、Cからmrubyのクラスメソッドを呼び出す方法をメモ

test.c
#include "mruby.h"
#include "mruby/string.h"
#include "mruby/dump.h"

static void
p(mrb_state *mrb, mrb_value obj)
{
  obj = mrb_funcall(mrb, obj, "inspect", 0);
  fwrite(RSTRING_PTR(obj), RSTRING_LEN(obj), 1, stdout);
  putc('\n', stdout);
}

int main()
{
  mrb_state* mrb;
  mrb_value val, val2;
  struct RClass *c_time;

  mrb = mrb_open();

  val = mrb_funcall(mrb, mrb_obj_value(mrb_module_get(mrb, "Time")), "gm", 7,
      mrb_fixnum_value(1970), // year
      mrb_fixnum_value(1),    // month
      mrb_fixnum_value(1),    // day
      mrb_fixnum_value(3),    // hour
      mrb_fixnum_value(4),    // min
      mrb_fixnum_value(5),    // sec
      mrb_fixnum_value(6)     // usec
      );

  p (mrb, val);

  val2 = mrb_funcall(mrb, val, "usec", 0);

  p (mrb, val2);

  mrb_close(mrb);

  return 0;
}
13
13
4

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
13
13