← homeProgramming (Програмування)

A bit about Ruby implementation types (CRuby (MRI), JRuby, Rubinius, TruffleRuby, mruby)

Ruby - object-oriented programming language with dynamic typing. It has several implementations that are specifically tailored for different environments and needs. CRuby The official interpreter - CRuby, also known a...

Table of contentsClick link to navigate to the desired location
This content has been automatically translated from Ukrainian.
Ruby - object-oriented programming language with dynamic typing. It has several implementations that are specifically tailored for different environments and needs.

CRuby

The official interpreter - CRuby, also known as Matz's (nickname of Yukihiro Matsumoto, the author of Ruby) Ruby Interpreter (MRI), is the official and most popular implementation of Ruby. It is written in C and serves as the standard for compatibility of various libraries (gem's).
CRuby is ideal for web development, especially when using the Ruby on Rails framework. It supports many libraries and has a large ecosystem and community.

JRuby

JRuby is an implementation of Ruby that runs on the Java Virtual Machine (JVM). It simplifies integration with Java libraries and allows integration into existing Java systems.
For example, JRuby can be used in large enterprise applications where integration with existing (legacy) Java systems is required.

Rubinius

Rubinius uses LLVM (Low Level Virtual Machine) for code compilation and provides multithreading at the code execution level. Rubinius aims to be fully compatible with CRuby, making it a decent option in some cases as an interpreter implementation. Rubinius can be used in the development of systems that require processing parallel requests (a large number), such as background tasks in web applications.

TruffleRuby

TruffleRuby is part of the Oracle Graal project and has high performance due to optimizations at the GraalVM virtual machine level. TruffleRuby is suitable for processing large volumes of data (for example - scientific research), where maximum execution speed and reduced computational resource costs are required.

mruby

mruby is a lightweight version of Ruby designed for embedded systems. It has a smaller size and consumes fewer resources, making it ideal for constrained environments. mruby can be used in embedded systems, such as microcontrollers or other portable devices, where memory consumption needs to be minimized.

Can Ruby run in the browser?

Ruby does not run directly in the browser like JavaScript does. There are several approaches and tools that allow Ruby to run in the browser.
Opal is a compiler that converts Ruby code to JavaScript, allowing you to write frontend logic in Ruby and then execute it in the browser as JavaScript. Opal processes Ruby code and converts it to optimized JavaScript, providing much of Ruby's functionality.
For example, on the Opal website, you can test the functionality and see an example of how the compiler works and how Ruby code is converted to JS and executed in the browser. A simple example:
Конвертуємо Ruby в JS за допомогою Opal
Конвертуємо Ruby в JS за допомогою Opal
Ruby:
def title(s)
  puts "title is #{s}"
end

puts title('hehe')
JS output:
Opal.queue(function(Opal) {/* Generated by Opal 1.8.2 */
  var $def = Opal.def, self = Opal.top, nil = Opal.nil;

  Opal.add_stubs('puts,title');
  
  
  $def(self, '$title', function $$title(s) {
    var self = this;

    return self.$puts("title is " + (s))
  });
  return self.$puts(self.$title("hehe"));
});
And the result:
title is hehe
WebAssembly (Wasm) is a tool that can compile code from a programming language implementation and run it in the browser. This is not a direct execution of Ruby in the browser. It is quite complex, but possible. One option might look like this: mruby is converted to C, and WebAssembly then converts the code to JavaScript, which can then be run in the browser.
There are also other options for compiling and running Ruby code in the browser. But it can be understood that currently there is no possibility of directly running Ruby code in the browser.

How to check which Ruby implementation I am using?

It's simple - run the command in the terminal to see the Ruby version, implementation, and architecture.
ruby -e "puts RUBY_DESCRIPTION"
In my case:
ruby 3.2.1 (2023-02-08 revision 31819e82c8) [x86_64-darwin22]
Under ruby it refers to the default implementation (CRuby).
For JRuby, the result will be approximately as follows:
jruby 9.2.11.1 (2.5.7) 2020-03-02 d8d4b94 OpenJDK 64-Bit Server VM 11.0.6+10-post-Ubuntu-1ubuntu118.04.1 on 11.0.6+10-post-Ubuntu-1ubuntu118.04.1 +jit [linux-x86_64]
So here we can clearly see the text jruby.

🔥 More posts

All posts
ZOMBIE in Ruby. What is it?
Programming (Програмування)May 3, '24 12:41

ZOMBIE in Ruby. What is it?

Ruby is a programming language. Everything is clear here. In the code of this language, you may e...

Programming (Програмування)May 7, '24 07:24

What is native machine code?

Native machine code is a type of software code that is executed directly by a computer's processo...

What is technical debt in IT projects?
Programming (Програмування)May 13, '24 06:17

What is technical debt in IT projects?

Technical debt is like a loan you take to speed up the development of a software product, but the...

What is "scope creep"?
Terms (Терміни)May 13, '24 07:20

What is "scope creep"?

Scope Creep (scope creep) - is the uncontrolled increase in the scope of work in a project, often...