Ruby2JS
Minimal yet extensible Ruby to JavaScript conversion.
Installation
# Gemfile
gem 'ruby2js'Or install directly:
gem install ruby2jsExamples
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 --presetFeatures
-
Filters - Transform Ruby methods to JavaScript equivalents (e.g.,
.each→.forEach) - ES Level Support - Target specific JavaScript versions (ES2020 through ES2025)
- 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_allRunning 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 serverThe site will be available at http://localhost:4000.
Release Process for Maintainers
- Update the version in both
packages/ruby2js/package.jsonandlib/ruby2js/version, ensuring they match. - Run
bundle exec rake release_core
License
MIT License - Copyright (c) 2009, 2025 Macario Ortega, Sam Ruby, Jared White
See LICENSE for details.