Skip to content

ruby2js/ruby2js

Repository files navigation

Ruby2JS

Minimal yet extensible Ruby to JavaScript conversion.

Gem Version CI

Documentation | Live Demo

Installation

# Gemfile
gem 'ruby2js'

Or install directly:

gem install ruby2js

Examples

Ruby2JS converts Ruby syntax to clean, readable JavaScript:

# Ruby                              # JavaScript
a = { age: 3 }                      # let a = {age: 3}
a.age += 1                          # a.age++

items.map { |x| x * 2 }             # items.map(x => x * 2)

class Dog < Animal                  # class Dog extends Animal {
  def bark                          #   bark() {
    puts "woof!"                    #     console.log("woof!")
  end                               #   }
end                                 # }

Quick Start

require 'ruby2js'

puts Ruby2JS.convert("a = {age: 3}; a.age += 1", preset: true)
# => let a = {age: 3}; a.age++

Command Line

ruby2js --preset file.rb
echo "puts 'hello'" | ruby2js --preset

Features

  • Filters - Transform Ruby methods to JavaScript equivalents (e.g., .each.forEach)
  • ES Level Support - Target specific JavaScript versions (ES5 through ES2022+)
  • Framework Integrations - Rails, Stimulus, React, Lit, and more
  • Live Demo - Try it in your browser (runs entirely client-side via Opal)

Contributing

Running Tests

bundle install
bundle exec rake test_all

Running the Website Locally

The ruby2js.com website (including the live demo) can be run locally from the docs folder:

cd docs
bundle install
yarn install
bundle exec rake            # build demo assets (Opal-compiled ruby2js, etc.)
bin/bridgetown start        # run the site's dev server

The site will be available at http://localhost:4000.

Release Process for Maintainers

  1. Update the version in both packages/ruby2js/package.json and lib/ruby2js/version, ensuring they match.
  2. Run bundle exec rake release_core

License

MIT License - Copyright (c) 2009, 2025 Macario Ortega, Sam Ruby, Jared White

See LICENSE for details.

About

Ruby to JavaScript conversion

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 15