Implementing mRuby (Part 1) | Ruby Creator Yukihiro "Matz" Matsumoto

Yukihiro Matsumoto explains mRuby's design: a C99 implementation that runs on minimal hardware like 48K LEGO Mindstorms, with a modular architecture separating compiler, VM, and libraries. It uses generational garbage collection to cut pause times and includes MRubyGem for packages.
Chapters
So the MRuby can be embedded in the systems or devices or applications. In order to do that, so the MRuby should be very portable. In fact, its core and the virtual machine is entirely written in C99 only. So you don't have to worry about the other APIs and system calls and anything like that. So, as a result, so, MRuby can run on any CPU that runs C99. In addition, oh, I mean, so, so you can run MRuby on Intel CPU or ARM or MIPS or SPARC or any other programming, any other CPU. In addition, so, MRuby does not depend on any operating systems. So you can, you can run MRuby on Linux, Android, OS X, iOS, Windows, or even without any operating systems. So you can run MRuby on this tiny board without any operating system. And then, The most other, the other important things is to provide some kind of software time. So when MRuby controls the, say, robots or some machines, so if it stops during the execution or different, during the walking, so for the garbage collection, for example. So it will crash. It will fall down. The robot will fall down. So the, we implemented the, the special garbage collection feature for MRuby, which is the, which is reduce the pause time. So the, the time spent in the collection of the object, which stops the real, the application So, to provide that, we implemented the generational incremental garbage collection, which the very recent CRuby provides same, same garbage collection. But we provide it to at least 2 years earlier. The MRuby also stands for minimal. That means that we have, we spend less Memory. In fact, the MRuby runs on very tiny computers. So these days we have, the PC has a lot of memory, even the 4 gig, 8 gig, or even the 12, 16 gigs or something like that. So that, that is huge memory. But— The computers in the embedded field tend to have very tiny memory. For example, the LEGO Mindstorms NXT, the second version of the LEGO Mindstorms, only has 48K of memory. That is quite small. And then the recent one has 40 meg of memory, which is very big compared to the 48K. But it's still small compared to the, you know, the several gigs of memory on PC. So the— to reduce the spending of memory, we designed MRuby that the compiler and the virtual machine and the class libraries can be separated and configurable. So, so in very tiny memory computers, We can separate compilers, so you can compile Ruby programs into bytecode, so that to execute the program, you only need that kind of compiled bytecode. So you can drop off that, all that compiler, to reduce the memory. Or you can drop off the unused class. So, OK, I don't use regular expression in, in my application, so I can drop the regular expression. Or, I don't use file I/O in this application. You can drop the file I/O. So you can configure the virtual machine suitable for your application. As a result, in the very minimal configuration— so MRuby only requires only 150K. That is very small. The spending, consumed memory is, depends on how big your application is. So this is, on this configuration, you can run only maybe 20, 40 lines of code or something. But at least you can run Ruby program on the computer with 150K. And on one attempt, very heavily modified mRuby virtual machine can run on the LEGO Mindstorms NXT, which has— which only has 48K. But to do that, to accomplish that, so we have— they have to strip down everything so that that virtual machine doesn't have the No strings. No strings. Only have numbers. So they can run 5 lines of line tracing Ruby program on the 48K of the Lego Mindstorms. But it's, it's a kind of a tricky way. But the usual way, we, we, minimum requirement of the memory is 150K. And then if you, if you have more memory, you can run more programs. So that leads to the modular system of the MRuby. As I said that, everything in MRuby is modular. Compiler, the virtual machine, and then, and the libraries are modular. So, in fact, we implemented the package system named MRubyGem, which is the counterpart of the Ruby gem for MRuby. Just because we don't have the ability to eval or load the program afterwards, or the dynamic loading of the C compiled object system, just because it's not included in C99. So the, everything in the MRuby gem is linked statically at compile time. OK. But you, to help to manage those MRB gems, so we implemented the tool named Mgem in Ruby. So you can install Mgem by using RubyGems. So you install using gem. Gem install Mgem. Install the program named Mgem. Mgem. So, and then you update the, the, the MRB gem data from, from the, the repository. Then you count the size, size of the library. So now, as of 2 days ago, so now we have 166 MRB gems in, in repository. Wow. And then we have even more MRB gems not registered in the repository. So we have a lot of gems that provides, for example, the regular expression in several ways, or maybe access to the database, or accessing the network, or the file I/O, or whatever. Access to the OpenCV or controlling the Linux resource, but with using cgroup feature of the Linux, or a lot of things. So the, by adding those gems, you can add functionality to, to MRuby gem that's suitable for your application. Well, and it's kind of easy to use MRuby. Just download everything from github.com/mruby/mruby. Then, the edit, oh, this is important here. Don't forget. The github://mruby/mruby. Then, if you want to configure your program, so the vi build_config.rb. So then type rake to compile everything. OK. So you have several programs compiled. The mIRB is one of them. mIRB is the interactive Ruby for MRuby. So you type in your program, so you get, you get your result. And you have the, like, when you compile, you can get everything compiled. Including gems in a library form, the libmruby.a. Or you can easily cross-compile to, say, so board computers or other CPU. So you can compile the, so Raspberry Pi binary on, it's a Linux PC or even on the, you know, on the Windows. Well. So the MRuby is fundamentally 1.9 compatible. So you haven't had yet that Ruby 2 fancy features yet. But we are working on it. And we are adopting Ruby 2 features gradually. So,
More from Tim

He Sold His Car to Build This Startup, Then Pitched It for $1M 😳 #Startups #AI

This Startup Moves Your Crypto BEFORE Hackers Can Steal It 😳 #Crypto #Cybersecurity

15 Months Without Refrigeration?! This Startup Thinks It Can Go Global 🥥 #FoodTech #Startups







