Ruby is used in a wide variety of projects today, but has gained popularity in web development with Ruby on Rails. With mruby, the latest language implementation, Ruby can be embedded into other applications.
The mruby project is hosted on Github. There are currently no binary distributions available, but it is easy enough to build mruby from source. mruby uses the bison parser generator, which we need to install before we can build from source.
Make sure you have Git installed on your system and then execute the following commands:
This will clone the repository from Github and compile mruby. After compilation has finished, you should have the mruby binaries in
bin/, library files in
lib/ and header files in
The compilation process generates three binaries in the
bin/ directory. Lets inspect them one by one.
This is the equivalent to the normal Ruby binary your have with MRI Ruby. You can execute mruby scripts or just check them for syntax errors.
mirb is the mruby variant of the interactive ruby shell, you probably already know from standard Ruby. It provides an easy and interactive interface where you can write ruby code and execute it on-the-fly.
The mrbc binary is the mruby bytecode compiler. It translates mruby scripts into either rite bytecode for the RiteVM (which is the name of the VM mruby uses) or transforms it into a C program. While mruby can directly execute Ruby scripts you can use mrbc to precompile the scripts into bytecode. This obviously makes execution of your programs faster, as the script does not have to be parsed every time you want to execute it.
Say you have a script called test.rb and want to compile it to bytecode and then execute it with mruby. All you need to do is the following:
mruby is designed to be embedded into other applications. It provides an easy to use C API which can be used to execute scripts or simply hard-coded strings or to extend mruby by providing custom classes that interface with your application. A "Hello World" like example for the C API is the following:
This small snippet creates a new instance of mruby using
mrb_open (defined in mruby.h) and then loads and executes the character array with ruby code using
mrb_load_string. In order to compile this code we need to tell gcc, to link the libmruby library. We also need to include the math library (-lm) which is not linked with libmruby.
There are several ways by which you can optimize mruby itself. For one you can force the mruby runtime to use float instead of double values for representing floating point number by simply defining the MRBUSEFLOAT constant as compiler option (-DMRBUSEFLOAT). Other options can be found in
By default only regular expressions are disabled.
mruby comes with a number of limitations to make it as light as possible. The probably most notable feature that has been left out, is the ability to require files. Requiring other files is an expensive operation and does not fit well into the design of mruby. Similarly forks and threads are not available for mruby scripts.
mruby is still in development, but there already are a number of good projects utilizing it. MobiRuby brings mruby to the iOS platform and allows you to build apps for mobile devices. mruby has a low-memory footprint and is in many ways similar to Lua. Lua is extensively used in scripting video games or as a way to influence httpd request processing in popular webservers like Apache, lighttpd or NginX. For Apache, there already is a mod_mruby that is developed as an alternative to Lua.