mruby is the latest Ruby implementation in an already quite long list:
And many other less known implementations.
mruby is designed to be modular and to be embedded, which means that it’s expected to live inside other software applications. In other words mruby’s goal is to make Ruby an embedded language. mruby is library that you link in other applications.
When talking about embedded languages, Lua is probably the most well known interpreted language. Lua is usually used for the purpose of offering a higher level language (scripting language) inside a software written in a low level language (usually C/C++). This is why Lua is very popular in the game industry to allow a scriptable interface and have parts of the code interpreted instead of requiring to recompile the code base against the target platform. According to this survey from 2009, Lua was by far the most popular scripting language for game developers.
Of course, Lua is also popular outside of the game industry and the reasons are simple to understand:
- very easily embedded (simple and well documented API)
- small footprint
- portable (runs almost everywhere)
That gives you an idea of what mruby is trying to be. As a matter of fact, this definition of Lua applies very well to mruby:
“Lua is dynamically typed, runs by interpreting bytecode for a register-based virtual machine, and has automatic memory management with incremental garbage collection, making it ideal for configuration, scripting, and rapid prototyping.”
Why not Lua?
This is a matter of taste and requirements. It should eventually boil down to the difference in the language designs.
Lua combines simple procedural syntax with powerful data description constructs based on associative arrays and extensible semantics.
Ruby is a dynamic, open source programming language with a focus on simplicity and productivity. It has an elegant syntax that is natural to read and easy to write.
So on one hand you have a very simple language with a focus on functional programming and on the other, a richer language (albeit more complex) with a main focus on Object Oriented programming.
Ruby being a full OO and richer language, you can organize your code differently and write more of your complex logic in a scriptable language. One could even potentially leverage the language to create its own Domain Specific Language in pure Ruby and create a “Rails like” experience within its application.
To be honest, I don’t think that Ruby via mruby will just replace Lua, but I do believe it will become a very interesting alternative for some use cases. Especially if mruby manages to perform as well as Lua with the same type of footprint. There is also the fact that mruby being a sponsored project from the Japanese government, I wouldn’t be surprised to see big Japanese companies experiment with embedding Ruby in their devices. After all, software is being added in every object we own, rapid development and being first to market is more critical than ever which makes mruby is a very attractive solution.
I wrote a simple hello world example to give you a place to start:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
Or the longer/more complex version:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
To compile and link the code:
To execute it:
1 2 3
To get started, you just need the mruby source code and a compiler. (I haven’t tried to compile mruby or my sample on Windows for this code, but I assume it would work just fine with Visual C++).
The above example is very trivial and takes a line of Ruby code:
that the linked mruby lib interprets. You can take the compiled file and use it on other machines using the same platform and the code will run fine, just like normal C code.
For a more complete example, take a look at the mruby’s standalone interpreter
It’s a bit too early to know if mruby will be successful or not. There are few things I will keep my eyes on.
To be a true alternative to Lua, mruby will have to interpret Ruby code much faster than what MRI is able to do right now. It will also need to keep the memory footprint really tiny. Ruby being a more complicated language than Lua, it might be tricky, but we’ll see.
While the idea of using Ruby has a macro language might get a lot of people excited, if documentation lacks or if the process is painful, the very same people might fallback to another language. Historically speaking, the MRI team has had issues with documentation and communication due to various factors. I hope that thanks to Matz experience and to the strong Ruby community, mruby will become a easy and efficient alternative to Lua or people wanting to embed one of the greatest interpreted languages.
Ruby outside of Rails
While Ruby on Rails really made Ruby popular, I’m always a bit dissapointed when I see how many popular projects the Ruby community has outside of Rails. Think about it, we have some awesome implementation such as JRuby and MacRuby which allow you to use an amazing amount of libraries to do the craziest thing one could imagine using the language they like the most. But yet, Rails is still by far the #1 Ruby project. Of course, there are many non-Rails related projects out there, but they don’t benefit from Rails’ aura.
What I hope with mruby is that it will allow developers to leverage the beauty of Ruby and to create other niches for people to have fun. Some people already started:
Yuichiro MASUI is working on having Ruby available on iOS and Android.
Ruby for Node.js
But there is plenty more to do, redis for instance now has/is about to be scriptabled via Lua, what about trying to use mruby to also support Ruby? What about scripting game logic and even full games in Ruby? What about mruby on a Raspberry pi? Ruby on my TV, fridge, car, AC, solar panel controller…
I will certainly be looking forward to people trying to reproduce the success of Rails in a different domain thanks to the Ruby language.