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:
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.
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]