Meet ruby.wasm

Finally, let's talk about Ruby and its wasm-ability.

One of the highlights of the Ruby 3.2 release was the WASI based WebAssembly support. What that meant is that from then on, the CRuby VM could be compiled into a ruby.wasm module and run in the browser!

You can check this yourself using the following HTML/JS code:

<html>
  <script src="https://cdn.jsdelivr.net/npm/@ruby/3.3-wasm-wasi@2.6.2/dist/browser.script.iife.js"></script>
  <script type="text/ruby">
    require "js"

    JS.global[:document].write "Hello, world, from #{RUBY_VERSION}!"
  </script>
</html>

Of course, not every Ruby code can be executed within a browser (or another Wasm runtime). Remember WASI? The current stable version of ruby.wasm supports only Preview 1, so no sockets, HTTP, and other cool stuff. Threads are also yet to come to Wasm (they only reached Phase 1 out of 5 phases of standardization).

Nevertheless, ruby.wasm has been actively utilized by the community. Today, we have a plenty of Ruby applications running in the browser:

These examples demonstrate how Ruby applications of different kinds could be ported to WebAssembly. But what about Rails (which is much more than just Ruby)? Can we achieve the same level of portability with Rails applications? The short answer is "Yes". Continue reading to learn how.


P.S. Thanks to the work of Yuta Saito, ruby.wasm is evolving fast. The Preview 2 support (including the Component Model) is in active development and is expected soon. Check out the recent report on the current state of ruby.wasm by Mr. Saito at EuRuKo 2024: SlideDeck.