Getting Started
Add the ruby2js
gem to your Gemfile:
bundle add ruby2js
or manually install the ruby2js
gem:
gem install ruby2js
If you’d like to use Ruby2JS with Node-based build tools, read these additional instructions.
Basic Usage
Simple:
require 'ruby2js'
puts Ruby2JS.convert("a={age:3}\na.age+=1")
With our recommended “preset” configuration:
puts Ruby2JS.convert("a={age:3}\na.age+=1", preset: true)
With just the functions filter:
puts Ruby2JS.convert('"2A".to_i(16)', filters: [:functions])
Host variable substitution:
puts Ruby2JS.convert("@name", ivars: {:@name => "Joe"})
Enable ES2021 support (default with the preset configuration):
puts Ruby2JS.convert('"#{a}"', eslevel: 2020)
Emit strict equality comparisons (aka ==
becomes ===
):
puts Ruby2JS.convert('a==1', comparison: :identity)
Emit nullish coalescing operators:
puts Ruby2JS.convert('a || 1', or: :nullish)
Emit underscored private fields (allowing subclass access):
puts Ruby2JS.convert('class C; def initialize; @f=1; end; end',
eslevel: 2020, underscored_private: true)
Continue to the next page to learn all about how to use the “preset” configuration or build your own.