DEV Community

Tomasz Wegrzanowski
Tomasz Wegrzanowski

Posted on

Open Source Adventures: Episode 59: What Opal Ruby is not

Opal Ruby compiles Ruby code into JavaScript, so it can run in environment where only JavaScript is supported, like most obviously browsers.

But before we get into what Opal Ruby is, let's start by saying what Opal Ruby is not, as these are the three most obvious use cases, and Opal Ruby is a poor fit for all three.

Opal Ruby is not a frontend framework

There are quite a few frontend frameworks that come with their languages - like Imba, Elm, and arguably Svelte. Opal Ruby does not come with any framework attached.

This is quite awkward, as for your frontend needs you'll need some kind of a framework, and unless that framework is Opal-aware, it will be difficult to use them together.

It's possible to build a framework on top of Opal Ruby, like notably Hypestack, which uses uses Opal and React. There are also some wrappers like opal-jquery, and I even created opal-d3 once upon a time, but I don't actively maintain it anymore.

Opal Ruby will not let you run existing Ruby code in the browser

Another thing we might want to do is run big Ruby codebases in the browser, like we can run Ruby on the JVM with JRuby.

This is not going to work. Opal Ruby has so many differences from regular Ruby, that your chance of any significant codebase working out of the box is slim.

This is not completely hopeless, as you could tweak your codebase a bit here and there to be able to run in both regular Ruby and Opal Ruby. I guess a good comparison would be writing a library that runs in both Python 2 and Python 3, I hope that doesn't give you any flashbacks.

There's also another way to run Ruby in a browser that can handle this use case better. Ruby can be compiled to WebAssembly, which as of now is a terrible platform that's only good for cryptomining malware, and lacks a lot of functionality we'd need to run real programs there, but there's some hope it might become good enough over the next few years. That's also what PyScript is doing.

Opal Ruby cannot replace JavaScript

Many environments come with bad languages, and there's been quite a few languages designed as a pretty much drop-in replacements, like:

  • CoffeeScript for pre-ES6 JavaScript
  • Kotlin for JVM
  • TypeScript for JavaScript
  • YueScript for Lua
  • for borderline cases JSX, Babel etc. for JavaScript
  • and so on

So Opal Ruby is not going to work like that. Any drop-in replacement attempt needs two things:

  • near seamless interoperability with the original language
  • low performance overhead

Opal Ruby unfortunately fails this. There's no way to get Ruby-like semantics on JavaScript VM, seamless JavaScript interoperability, and good performance at the same time.

You can definitely write some frontend code in Opal Ruby, but even in the best case the codebase will be some Opal Ruby / JavaScript hybrid with a lot of wrappers, and a lot of dropping to plain JavaScript for performance.

If you really want a Ruby-like language that can serve as a drop-in replacement for JavaScript, CoffeeScript 2 or Imba 2 at least try to fulfil that role.

Coming next

Now that we established what Opal Ruby is not, over the next few episodes we'll explore what it is.

Top comments (1)

Collapse
 
taw profile image
Tomasz Wegrzanowski

I should definitely check out Ruby2JS as well, somehow I forgot about it.