All original content is created in Ukrainian. Not all content has been translated yet. Some posts may only be available in Ukrainian.Learn more

A little about the types of Ruby implementations (CRuby (MRI), JRuby, Rubinius, TruffleRuby, mruby)

Post cover: A little about the types of Ruby implementations (CRuby (MRI), JRuby, Rubinius, TruffleRuby, mruby)
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 (gems).
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 execution level. Rubinius aims to be fully compatible with CRuby, making it a good option in some cases as an interpreter implementation. Rubinius can be used in the development of systems that require processing parallel requests (in large quantities), 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, as is possible with JavaScript. There are several approaches and tools that allow Ruby to be 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 into optimized JavaScript, providing much of Ruby's functionality.
For example, on the Opal website, you can test the functionality and visually 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 then WebAssembly 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 a 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 like this:
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.

This post doesn't have any additions from the author yet.

18 Apr 05:27

Fixing ActiveRecord::ProtectedEnvironmentError in the local environment

meme code
meme code@memecode
18 Apr 06:06

Analyzing the PG::ObjectInUse error (Ruby on Rails)

meme code
meme code@memecode
27 Apr 09:29

What are CC and BCC in emails? What are they for and how to use them?

meme code
meme code@memecode
What is a prompt and prompt engineering?
03 May 12:08

What is a prompt and prompt engineering?

meme code
meme code@memecode
ZOMBIE in Ruby. What is it?
03 May 12:41

ZOMBIE in Ruby. What is it?

meme code
meme code@memecode
03 May 13:13

What is the Garbage Collector in Ruby? How does it work and what is GC used for?

meme code
meme code@memecode
07 May 07:24

What is native machine code?

meme code
meme code@memecode
Enabling YJIT in Ruby 3.2.1 (Ruby on Rails)
08 May 07:57

Enabling YJIT in Ruby 3.2.1 (Ruby on Rails)

meme code
meme code@memecode
09 May 12:43

[Fix] Rails Admin - undefined local variable or method javascript_importmap_shim_nonce_configuration_tag

meme code
meme code@memecode
What is technical debt in IT projects?
13 May 06:17

What is technical debt in IT projects?

meme code
meme code@memecode
13 May 07:11

What does scope mean in IT project management?

meme code
meme code@memecode
What is "scope creep"?
13 May 07:20

What is "scope creep"?

meme code
meme code@memecode