Getting started with mruby (Part 2)

by Yuji Yokoo

In the previous post, I have built mruby in a vagrant environment. This post continues from there. 

Running ruby programs with mruby

Obviously, you use mruby to run ruby scripts. There are a few different ways to do this. First, there is mirb's interactive method:

Next, there is the mruby interpreter method:

You could also run pre-compiled bytecode with mruby:

If you look closely, you can see the RiteVM bytecode starts with "RITE0009000000090000MATZ" here :)

You could also generate bytecode to compile in your C program:

The generated helloworld_bytecode.c looks like this:

And the C program to utilise this generated code:

It can be compiled like this:

Also, you could generate a C function:

The generated helloworld.c looks like this:

And the main program that calls this function:

It can be compiled and run as following:

You could also directly embed mruby code in C as a literal string:

Then compile and run like this: