# Ruby Next

> A transpiler and a collection of polyfills for supporting new and upcoming Ruby features in older versions and alternative implementations.

- URL: https://evilmartians.com/opensource/ruby-next
- GitHub: https://github.com/ruby-next/ruby-next
- Authors: Vladimir Dementyev

---

Ruby Next is a transpiler and collection of polyfills for supporting both new and upcoming Ruby features (APIs and syntax) in older versions and alternative implementations of Ruby.

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.

*Read also Evil Martians' article*: https://evilmartians.com/chronicles/ruby-next-make-all-rubies-quack-alike

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:

```sh
# 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:

```ruby
# 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:

```ruby
require "ruby-next"
```

(And activate the refinement in every file where you want to use it):

```ruby
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:

```ruby
require "ruby-next/core_ext"
```

## Transpiling

Ruby Next allows you to transpile edge Ruby syntax to older versions.

Installation:

```ruby
# Gemfile
gem "ruby-next"

# gemspec
spec.add_dependency "ruby-next"
```

```sh
# or install globally
gem install ruby-next
```
