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:


Getting started with mruby (Part 1)

by Yuji Yokoo


I have recently been playing around with mruby. I would like to share some of my notes here.

Overview

There are lots of mruby information available, so I won't go into fine details but simply, it is a "new ruby" from Yukihiro Matsumoto, aka Matz. It is a small, compact, modular ruby intended for embedded environments, like home appliances and portable electronics. It runs on RiteVM and is not intended to be fully compatible with YARV or MRI. It currently does not have features like file IO (no "require"), regular expression, and "eval". mruby code can be compiled into C code.

Environment

To make things easy, I am using a vagrant box. It was a while ago, but I think I downloaded the "Debian Squeeze amd64 (with Puppet, Chef and VirtualBox 4.2.1)" from http://www.vagrantbox.es/ :

Now you should be able to use the vagrant environment. If you want to follow the steps here and upload code to an openwrt router later, you have to change the networking to the bridged mode in Vagrantfile but it's optional, and you might not have an openwrt-compatible router to play with.

Building mruby

Building mruby is extremely easy (well, it was, for me):

This failed because it's missing bison. Let's install bison and retry:

should do it. You can try the following to check that it's worked:

It's working!