opal is a free, open source browsers & extensions project written in Ruby and released under MIT. It has 4,926 GitHub stars, 336 forks and 152 open issues, and was last pushed 5 days ago. On this registry it ranks #62 of 101 tracked projects in Browsers & Extensions, with 5 head-to-head comparisons available.

What is opal?

Opal is an MIT-licensed Ruby to JavaScript source-to-source compiler, distributed as a Ruby gem, that lets developers write Ruby and run the resulting JavaScript in the browser, Node.js, Deno, Bun, or QuickJS.

What it is

Opal is a source-to-source compiler that turns Ruby into plain JavaScript, and it ships with its own implementation of the Ruby corelib and stdlib rather than leaning on a host interpreter. Compilation happens ahead of time, so there is no VM to download and no interpreter loop at runtime. The implementation is validated against ruby/spec, the same suite CRuby uses, and the limits a JavaScript host imposes are catalogued in docs/reference/unsupported_features.md. The compiler is itself written in Ruby and builds to opal-parser.js, so Opal can compile Ruby inside a browser as well as on a server.

The problem it solves is code duplication between the two halves of a web application. A team writes Ruby once and shares the same gem, the same objects, and the same specs on both sides of the wire, instead of maintaining a parallel hand-written JavaScript implementation of logic that already exists in Ruby. Two-way interop closes the remaining gaps: Ruby can call JavaScript, JavaScript can call Ruby, and native JavaScript classes can be treated as Ruby ones. The project lives in the Ruby ecosystem and emits into the JavaScript ecosystem, which is why it carries topics for ruby, javascript, compiler, nodejs, and browser.

Key capabilities

  • Command-line compilation through opal --compile app.rb > app.js, with the Opal runtime included by default and --no-opal available to leave it out.
  • Programmatic compilation from Ruby with Opal.compile, which turns a Ruby string into a JavaScript string, and Opal::Builder.build('opal') for the runtime.
  • Whole-application builds through Opal::Builder, which resolves require dependencies and writes output with File.binwrite in binary mode.
  • Evaluation of Ruby directly from HTML through opal-parser, with no build step, which ships the compiler to the client.
  • Two-way JavaScript interop, including treating native JavaScript classes as Ruby ones, documented under Interfacing with JavaScript.
  • A Ruby corelib and stdlib implementation validated against ruby/spec, with unsupported features documented rather than left implicit.
  • Framework integrations for Rails, Sinatra, Roda through Sprockets, and static applications.

Who uses it and how

  • Teams that want the same gem, the same objects, and the same specs running on the server and in the client, sharing logic instead of rewriting it.
  • Browser applications that compile Ruby to a .js file with the CLI and load it from an HTML page, where the page encoding must be set to UTF-8 because Opal's string handling depends on it.
  • Rails, Sinatra, Roda, and static-site projects that use the documented integrations rather than wiring the compiler up by hand.
  • Developers trying the language in a browser tab with opal-parser, which the README frames as a fast way to try Opal rather than a way to deploy it.

Getting started

Install the gem with gem install opal or add gem 'opal' to a Gemfile; the latest release is from the 1.8 series, while master tracks the unreleased 2.0.0dev and can be followed with gem 'opal', github: 'opal/opal'. Verify the install with opal -v and opal -e "puts 'Hello from Opal'", which compiles the Ruby and runs the result on Node.js.

How it compares

This registry provides no list of paid products that Opal replaces, and the facts name no comparable tool. Opal stands alone in this registry on that basis.

When to use it — and when not to

Opal requires Ruby to install: the gemspec declares >= 2.3, CI exercises Ruby 3.0 through 4.0 and JRuby, and 3.x or newer is the sensible choice, so a self-hoster operates a Ruby toolchain alongside whichever JavaScript runtime the output targets. The upcoming 2.0 series targets ES2021 with no transpilation fallback, so anyone who must support an older engine has to stay on Opal 1.x, and Internet Explorer is not supported at all. Opal is the wrong pick for anyone who needs a Ruby interpreter without host-imposed limits, since those limits are real and documented, or for anyone planning to deploy opal-parser in production, because it ships the compiler to the client.

project readme (upstream, from github) — read inline

Opal logo
Opal

Opal is a Ruby to JavaScript source-to-source compiler, shipping with an implementation of the Ruby corelib and stdlib.

Gem Version Build Status Documentation Slack

Write Ruby, run it anywhere JavaScript runs — the browser, Node.js, Deno, Bun, QuickJS.

Why Opal

  • A real Ruby implementation. Opal ships its own corelib and stdlib and is validated against [ruby/spec][ruby-spec], the same suite CRuby uses. A JavaScript host imposes limits — those are catalogued in Unsupported Features.
  • Source-to-source, no runtime interpreter. Ruby compiles to plain JavaScript ahead of time, so there is no VM to download and no interpreter loop at runtime.
  • Two-way JavaScript interop. Call JS from Ruby and Ruby from JS, and treat native JS classes as Ruby ones. See [Interfacing with JavaScript][js-interface].
  • Share code between server and client. The same gem, the same objects, the same specs, on both sides of the wire.
  • It compiles itself. The compiler is written in Ruby and builds to opal-parser.js, so it also runs inside the browser.

Installation

Opal needs Ruby. The gemspec declares >= 2.3; CI exercises Ruby 3.0 through 4.0 and JRuby, so 3.x or newer is the sensible choice.

gem install opal

Or in your Gemfile:

gem 'opal'

This installs the latest release, from the 1.8 series. The master branch is 2.0.0dev and is not yet released; to track it:

gem 'opal', github: 'opal/opal'

Then check it:

opal -v
opal -e "puts 'Hello from Opal'"

The second command prints Hello from Opal — it compiled the Ruby and ran the result on Node.js.

Documentation

Usage

Compiling Ruby with the CLI

Contents of app.rb:

puts 'Hello world!'

Then from the terminal:

opal --compile app.rb > app.js

The Opal runtime is included by default; skip it with --no-opal.

The resulting JavaScript file runs from an HTML page. Set the page encoding to UTF-8 — Opal's string handling depends on it:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <script src="app.js"></script>
  </head>
  <body>
  </body>
</html>

Open the page in a browser and check the JavaScript console.

Compiling Ruby from Ruby

Opal.compile compiles a string of Ruby into a string of

require 'opal'

Opal.compile("puts 'wow'")
# => "Opal.queue(function(Opal) { ... return self.$puts(\"wow\") ... });\n"

That output alone is not runnable — it needs the Opal runtime/corelib. Opal::Builder builds the runtime:

Opal::Builder.build('opal')  # => "(function() { ... })()"

or an entire app, resolving require dependencies:

builder = Opal::Builder.new
builder.build_str('require "opal"; puts "wow"', '(inline)')
File.binwrite 'app.js', builder.to_s  # must use binary mode for writing

Compiling Ruby from HTML

opal-parser evaluates Ruby directly from your HTML files, with no build step:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <script src="https://cdn.opalrb.com/opal/current/opal.js"></script>
    <script src="https://cdn.opalrb.com/opal/current/opal-parser.js" onload="Opal.load('opal-parser')"></script>

    <script type="text/ruby">
      puts "hi"
    </script>
  </head>
  <body>
  </body>
</html>

Open the page and check the JavaScript console.

NOTE: this ships the compiler to the client. It is a fast way to try Opal, not a way to deploy it.

Runtime support

The upcoming 2.0 targets ES2021: compiled output and the Opal runtime assume an ES2021-capable engine, and there is no transpilation fallback. If you need to support an older engine, stay on Opal 1.x. See the migration guide for the full rationale.

That means:

  • Firefox, Chrome, Safari, Edge — current and previous stable versions
  • Node.js, Deno, Bun, QuickJS, and more — see Runners

Internet Explorer is not supported. Problems on any engine listed above should be reported as bugs.

Contributing

HACKING.md is the contributor guide — prerequisites, bin/setup, running the spec suites, benchmarking, and profiling:

The short version, once you have cloned the repo:

bin/setup
bundle exec rake

Code layout

  • lib/ — the Opal parser and compiler. Runs in your Ruby environment, and is also built for the browser as opal-parser.js.
  • opal/ — the runtime and corelib, written in Ruby and JavaScript. Runs in the JavaScript environment.
  • stdlib/ — Opal's implementation of Ruby's stdlib (StringScanner, Date, Observable, …). Optional, runs in the JavaScript environment.

Versioning

Opal will broadly follow semver as a version policy, trying to bump the major version when introducing breaking changes. Being a language implementation we're also aware that there's a fine line between what can be considered breaking and what is expected to be "safe" or just "additive". Moving forward we'll attempt to better clarify what interfaces are meant to be public and what should be considered private.

The master branch is currently 2.0.0dev — unreleased. The latest released gem is on RubyGems.

Community

Contributors

This project exists thanks to all the people who contribute. contributors

Backers

Thank you to all our backers! 🙏 [Become a backer]

Become a Backer Button Sponsor 1 Sponsor 2 Sponsor 3 Sponsor 4 Sponsor 5 Sponsor 6 Sponsor 7 <img src="https://opencollective.com/opal/sponsor/8/avatar.svg" alt="S

readme truncated — read the full docs on github

Frequently asked questions

Is opal free to use?

opal is open source under the MIT licence. There is no licence fee and no seat count — you can self-host it or, where the project offers one, pay a vendor for a managed version instead.

What does opal do?

Ruby ♥︎ JavaScript

What is opal written in?

opal is primarily written in Ruby. Its source is publicly available at https://github.com/opal/opal, and it has 4,926 GitHub stars.