Ruby Next is a transpiler and collection of polyfills for supporting both the latest and upcoming Ruby features (APIs and syntax) in older versions and alternative implementations. For example, you can use pattern matching and Kernel#then
in Ruby 2.5 or mruby.
Who might be interested in Ruby Next?
- Ruby gem maintainers who want to write code using the latest Ruby version, but still support older ones.
- Application developers who want to give new features a try without waiting for a final release (or, more often, for the first patch).
- Users of non-MRI implementations such as mruby, JRuby, TruffleRuby, Opal, RubyMotion, Artichoke, Prism.
Ruby Next also aims to help the community to assess new, experimental, MRI features by making it easier to play with them. That’s why Ruby Next implements the master
features as fast as possible.
Overview
Ruby Next consists of two parts: core and language.
Core provides polyfills for Ruby core class APIs via Refinements (default strategy) or core extensions (optionally, or for refinement-less environments).
Language is responsible for transpiling edge Ruby syntax into older versions. This can be done programmatically, or via CLI. It also could be done at runtime.
Currently, Ruby Next supports Ruby versions 2.2+, including JRuby 9.2.8+ and TruffleRuby 20.1+ (with some limitations). Support for EOL versions (<2.5) slightly differs though.
Quick start
The quickest way to start experimenting with Ruby Next is to install the gem and run a sample script. For example:
# Install Ruby Next globally
$ gem install ruby-next
# Call ruby with -ruby-next flag
$ ruby -ruby-next -e "
def greet(val) =
case val
in hello: hello if hello =~ /human/i
'🙂'
in hello: 'martian'
'👽'
end
puts greet(hello: 'martian')
"
=> 👽
Using only polyfills
First, install this gem:
# Gemfile
gem "ruby-next-core"
# gemspec
spec.add_dependency "ruby-next-core"
Note: we use a different distribution gem, ruby-next-core
, to provide a zero-dependency, polyfills-only version.
Then, all you need is to load Ruby Next:
require "ruby-next"
(And activate the refinement in every file where you want to use it):
using RubyNext
Ruby Next only refines core classes if necessary; thus, this line wouldn’t have any effect in the edge Ruby code.
Note: Even if the runtime already contains a monkey-patch with backported functionality, we consider this method dirty and activate the refinement for it. Thus, you always have predictable behaviour. That’s why we recommend using refinements for gem development.
Alternatively, you can go with monkey-patches. Just add this line:
require "ruby-next/core_ext"
Transpiling
Ruby Next allows you to transpile edge Ruby syntax to older versions.
Installation:
# Gemfile
gem "ruby-next"
# gemspec
spec.add_dependency "ruby-next"
# or install globally
gem install ruby-next