MRI

From Rosetta Code
Revision as of 04:21, 17 September 2011 by rosettacode>Kernigh (Describe Global VM Lock, core library, YARV and such.)
MRI is an implementation of Ruby. Other implementations of Ruby.
<lang ruby>if RUBY_VERSION >= "1.9"
 p RUBY_ENGINE
 # => "ruby"
end</lang>

Matz's Ruby Implementation or MRI refers to the original Ruby interpreter by Yukihiro Matsumoto, the inventor of Ruby. Matz and contributors wrote the interpreter in C language; MRI is also known as C Ruby or CRuby, by analogy with CPython.

Other implementations of Ruby follow MRI. When an example on Rosetta Code works with Ruby 1.8.7, this can be MRI 1.8.7, or anything else that implements the same language.

  • Ruby 1.8.7 adds Array#permutation method. This means that MRI 1.8.7 has Array#permutation, and other implementations of Ruby 1.9.2 must also have Array#permutation.
  • MRI implements Array#sort with quicksort. Other implementations of Ruby might use different sorting algorithm.

MRI has the Global VM Lock, alias Giant VM Lock or GVL. Each thread that runs Ruby code must hold this exclusive lock; therefore, multiple threads can use only CPU. (Contrast JRuby, where multiple threads might use multiple CPUs.)

Features

MRI has

  1. an interpreter,
  2. a mark-and-sweep garbage collector, and
  3. the core and standard libraries.

The entire core library is in C. The standard library is a mix of Ruby code and C extensions.

MRI 1.8 has a somewhat slow interpreter. MRI 1.9 has a new interpreter called Yet Another Ruby VM (YARV); it translates Ruby source code to an internal bytecode, then interprets the bytecode. Ruby code can run a few times faster in MRI 1.9 than in MRI 1.8. Ruby code remains slower than C code. For example, MRI 1.9.3 changes its 'date' package from Ruby code to a C extension; this gives better performance.