Register

Ruby2JS is for Ruby developers who want to produce JavaScript that looks hand-crafted, rather than machine generated. You can convert Ruby-like syntax and semantics as cleanly and “natively” as possible. This means that (most of the time) you’ll get a line-by-line, 1:1 correlation between your source code and the JS output.
For example:
class MyClass
# Cowabunga, dude!
def my_method(str)
ret = "Nice #{str} you got there!"
ret.upcase()
end
end
puts MyClass.new.my_method('pizza')
will get converted to:
class MyClass {
// Cowabunga, dude!
myMethod(str) {
let ret = `Nice ${str} you got there!`;
return ret.toUpperCase()
}
}
console.log((new MyClass).myMethod("pizza"))
Filters may be provided to add Ruby-specific or framework specific behavior. Filters are essentially macro facilities that operate on an AST (Abstract Syntax Tree) representation of the code.
Ruby2JS can be used to write back-end code for execution by Node, or for the front-end in a variety of configurations including Rails, esbuild, Rollup, Snowpack, Vite, and Webpack. Our examples and installation instructions will help you get set up in no time.
A note about this site: many of the examples are interactive. If you change the Ruby code above, the JavaScript code below it will be updated to match. If you open your browser’s JavaScript console, you can see the results of executing this script.
This example has been pre-configured with ECMAScript 2020 support and the functions, camelCase and return filters. Other examples may be configured differently. The Try It Online! button above will take you to a page where you can select your own configuration.