{{Short description|Lightweight Ruby language implementation}} {{lowercase title}} {{Infobox Software | name = mruby | logo = Mruby logo red.svg | logo size = 200px | developer = Yukihiro Matsumoto et al. | operating system = Cross-platform | latest release version = {{wikidata|property|preferred|references|edit|P348|P548=Q2804309}} | latest release date = {{Start date and age|{{wikidata|qualifier|preferred|single|P348|P548=Q2804309|P577}}|df=yes}} | programming language = C and Ruby | genre = Ruby programming language interpreter | license = MIT License<ref>{{cite web |title=LICENSE |url=https://github.com/mruby/mruby/blob/master/LICENSE |website=Github |accessdate=6 September 2019}}</ref> | website = {{official URL}} | released = {{Start date and age|2012|04|20}} | standard = ISO/IEC 30170:2012 }} '''mruby''' is an interpreter for the Ruby programming language with the intention of being lightweight and easily embeddable.<ref name="GitHub">{{cite web | url = https://github.com/mruby/mruby | title = mruby/mruby | accessdate = 2018-04-30 | publisher = GitHub}}</ref><ref>[http://matt.aimonetti.net/posts/2012/04/20/mruby-and-mobiruby/ mruby and MobiRuby announced]</ref> The project is headed by Yukihiro Matsumoto, with over 100 contributors currently working on the project.

==Features== mruby 1.0 supports the Ruby 2.1 core API, 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<ref>{{Cite web|url=http://mruby.org/releases/2018/12/11/mruby-2.0.0-released.html|title=mruby 2.0.0 released|website=mruby.org|access-date=2019-04-01}}</ref> 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.<ref>{{Cite web|url=http://mruby.org/docs/articles/executing-ruby-code-with-mruby.html|title=Executing Ruby code with mruby|website=mruby.org|access-date=2019-04-01}}</ref>

mruby also aims<ref name="GitHub" /> to be compliant with the ISO/IEC 30170:2012 standard.<ref>{{Cite web|url=http://www.iso.org/cms/render/live/en/sites/isoorg/contents/data/standard/05/95/59579.html|title=ISO/IEC 30170:2012|website=ISO|language=en|access-date=2019-04-01}}</ref>

==Examples==

===Calling mruby from C===

<syntaxhighlight lang="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; } </syntaxhighlight>

Assuming that the mruby library and headers are installed, the program can be compiled and executed by running the following commands from the terminal:<ref>{{cite web | url = http://matt.aimonetti.net/posts/2012/04/25/getting-started-with-mruby/ | title = Getting started with mruby | accessdate = 2013-12-29 | last = Aimonetti | first = Matt | date = 2012-04-25}}</ref>

<pre> $ cc example.c -lmruby -lm -o example $ ./example </pre>

===Precompiled Bytecode===

mruby includes a minimalistic virtual machine used to execute mruby bytecode, nicknamed ''RiteVM'':

<pre> $ mrbc test.rb $ mruby -b test.mrb </pre>

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.<ref>{{cite web | url = https://geekmonkey.org/an-introduction-to-mini-ruby/ | title = An introduction to Mini Ruby | accessdate = 2013-12-29 | author = geekmonkey | date = 2012-10-30}}</ref>

==References== {{reflist}}

{{Ruby programming language}}

Category:Interpreters (computing) Category:Ruby (programming language) Category:Software using the MIT license