Ruby2JS
Ruby2JS is an extensible Ruby to modern JavaScript transpiler you can use in production today.
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. A preset
option lets you load the most common filters and a recent ES level for broad code compatibility between Ruby2JS projects.
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. And you can try writing your own Ruby2JS code and see how it converts!