mruby
mruby is an interpreter for the Ruby programming language with the intention of being lightweight and easily embeddable.[3][4] The project is headed by Yukihiro Matsumoto, with over 100 contributors currently working on the project.
Developer(s) | Yukihiro Matsumoto et al. |
---|---|
Initial release | April 20, 2012 |
Stable release | 2.1.0
/ November 19, 2019[1] |
Repository | |
Written in | C and Ruby |
Operating system | Cross-platform |
Standard(s) | ISO/IEC 30170:2012 |
Type | Ruby programming language interpreter |
License | MIT License[2] |
Website | mruby |
Features
mruby 1.0 supports the Ruby 2.1 core AP, but none of the standard library. As well as being able to execute most basic Ruby code, mruby also features a bytecode compiler and virtual machine, as well as the ability to be easily embedded and integrated into C or C++ code, in a similar manner to Lua or Tcl.
mruby 2.0.0[5] adds support for several Ruby 2.x methods beyond Ruby 2.1. v2.0.0 also changed to variable length bytecode instructions format.
mruby bytecode can be embedded in C code, and thus, can be compiled into a standalone executable.[6]
mruby also aims[3] to be compliant with the ISO/IEC 30170:2012 standard.[7]
Examples
Calling mruby from C
#include <stdio.h>
#include <mruby.h>
#include <mruby/compile.h>
int main(void) {
mrb_state *mrb = mrb_open();
char code[] = "5.times { puts 'mruby is awesome!' }";
printf("Executing Ruby code with mruby:\n");
mrb_load_string(mrb, code);
mrb_close(mrb);
return 0;
}
Assuming that you have mruby installed and in your path, the following program can be compiled and executed by running the following command from your terminal:[8]
$ cc example.c -lmruby -lm -o example $ ./example
Precompiled Bytecode
mruby includes a minimalistic virtual machine used to execute mruby bytecode, nicknamed ritevm:
$ mrbc test.rb $ mruby -b test.mrb
The first command compiles Ruby code to mruby bytecode, creating a file called "test.mrb", which can then be executed by appending the "-b" flag to the normal interpreter arguments.[9]
References
- "mruby releases". Retrieved 8 January 2020 – via GitHub.
- "LICENSE". Github. Retrieved 6 September 2019.
- "mruby/mruby". GitHub. Retrieved 2018-04-30.
- mruby and MobiRuby announced
- "mruby 2.0.0 released". mruby.org. Retrieved 2019-04-01.
- "Executing Ruby code with mruby". mruby.org. Retrieved 2019-04-01.
- 14:00-17:00. "ISO/IEC 30170:2012". ISO. Retrieved 2019-04-01.CS1 maint: numeric names: authors list (link)
- Aimonetti, Matt (2012-04-25). "Getting started with mruby". Retrieved 2013-12-29.
- geekmonkey (2012-10-30). "An introduction to Mini Ruby". Retrieved 2013-12-29.