Skip to content
magewhopper edited this page Jul 5, 2012 · 10 revisions
#include <stdlib.h>
#include <stdio.h>

/* Include the mruby header */
#include <mruby.h>
#include <mruby/proc.h>
#include <mruby/data.h>
#include <mruby/compile.h>

int main(void)
{
  struct mrb_parser_state *p;
  mrb_state *mrb = mrb_open();
  char code[] = "p 'hello world!'";
  printf("Executing Ruby code from C!\n");

  p = mrb_parse_string(mrb, code, NULL);
  int n;
  n = mrb_generate_code(mrb, p->tree);
  mrb_run(mrb, mrb_proc_new(mrb, mrb->irep[n]), mrb_top_self(mrb));
  if (mrb->exc) {
    mrb_p(mrb, mrb_obj_value(mrb->exc));
  }
  return 0;
}

compile & link

$ gcc -Iinclude hello.c lib/libmruby.a -lm -o hello.out

execute

$ ./hello.out
Executing Ruby code from C!
"hello world!"

For more information on getting started, you can read this introduction blog post.

Clone this wiki locally