Yearning to use some of the new cool features in Ruby 2.7 while using 2.3.x?
Have some legacy code in Ruby 1.8 but can't live without flat_map?
This gem is for you!
The goal of 'backports' is to make it easier to write ruby code that runs across different versions of Ruby.
Note: Next major version (X-mas 2020) will drop support for Ruby < 2.2.
For example, if you want to use transform_values and transform_keys, even in Ruby implementations that don't include it:
require 'backports/2.4.0/hash/transform_values'
require 'backports/2.5.0/hash/transform_keys'
This will enable Hash#transform_values and Hash#transform_keys, using the native versions if available or otherwise provide a pure Ruby version.
To bring all the backports for a given Class/Module, you can specify only that Class:
require 'backports/2.3.0/hash'
This will make sure that Hash responds to dig, fetch_values, <, <=, >, >= and to_proc
You can load all backports up to a specific version. For example, to bring any version of Ruby mostly up to Ruby 2.7.0's standards:
require 'backports/2.7.0'
This will bring in all the features of 1.8.7 and many features of Ruby 1.9.x all the way up to Ruby 2.7.0 (for all versions of Ruby)!
You may require 'backports/latest' as a
shortcut to the latest Ruby version supported.
Note: For production / public gems, it is highly recommended you only require the backports you need explicitly.
Note: Although I am a Ruby committer, this gem is a personal project and is not endorsed by ruby-core.
Goals for backported features:
- Won't break older code
- Pure Ruby (no C extensions)
- Pass ruby/spec
Let's be a bit more precise about the "breaking code" business. It is of
course entirely possible that code will break. In particular, you may be
distinguishing parameters with duck typing, but a builtin class may, in the
future, be responding to a particular call. Here's an example from Rails
that is relying on the fact that Proc and Method respond to :to_proc and Hash
isn't. That is, until Ruby 2.3... This old version of Rails therefore won't
work on Ruby 2.3, or on older Rubies with that the 2.3.0/hash/to_proc
loaded...
For Ruby < 2.0, there are some real incompatibilities. For example,
Module::instance_methods which returns strings in 1.8 and symbols in 1.9. No
change can be made without the risk of breaking existing code. Such
incompatibilities are left unchanged, although you can require some of these
changes in addition (see below).
backports can be installed with:
gem install backports
To use:
require 'rubygems'
# For only specific backports:
require 'backports/1.9.1/kernel/require_relative'
require 'backports/2.0.0/enumerable/lazy'
# For all backports up to a given version
require 'backports/1.9.2' # All backports for Ruby 1.9.2 and below
Note: about a dozen of backports have a dependency that will be also loaded. For example, the backport of Enumerable#flat_map uses flatten(1), so if required from Ruby 1.8.6 (where Array#flatten does not accept an argument), the backport for Ruby's 1.8.7 flatten with an argument will also be loaded.
With bundler, add to your Gemfile:
gem 'backports', :require => false
Run bundle install and require the desired backports. Compatible with Ruby
itself, JRuby and Rubinius.
- intersection
- clamp(with range)
- +<=>+
- filter_map
- tally
- produce(class method)
- floor,- ceil
- difference,- union
- to_h(with block)
- chain
- to_h(with block)
- merge,- merge!/- update(with multiple arguments)
- to_h(with block)
- then
- +<<+, +>>+
- +<<+, +>>+
- cover?(with- Rangeargument)
- append,- prepend
- children,- each_child
- any?,- all?,- none?,- one?(with pattern argument)
- slice
- transform_keys
- sqrt
- allbits?,- anybits?and- nobits?
- yield_self
- attr,- attr_accessor,- attr_reader,- attr_writer(now public)
- define_method,- alias_method,- undef_method,- remove_method(now public)
- delete_prefix,- delete_prefix!
- delete_suffix,- delete_suffix!
- undump
- new(with- keyword_init: true)
- clamp
- sum
- uniq
- compact,- compact!
- transform_values,- transform_values!
- match?
- match?
- dup
- bsearch_index
- dig
- chunk_while
- grep_v
- dig
- fetch_values
- to_proc
- <=, <, >=, >
- negative?
- positive?
- unary + and -
- dig
- slice_after
- slice_when
- prev_float
- next_float
- itself
- curry
- super_method
- unicode_normalize
- unicode_normalize!
- unicode_normalize?
- to_h
- bit_length
- to_h
- bit_length
- include(now public)
- bsearch
- lazy
- all methods
- default_proc=(with nil argument)
- to_h
- bsearch
- to_h
- NULL
- advise(acts as a noop)
- write,- binwrite
- byteslice
- prepend
- rotate, rotate!
- keep_if, select!
- product(with block)
- repeated_combination,- repeated_permutation
- sort_by!
- uniq, uniq!(with block)
- to_r
- home
- chunk
- flat_map,- collect_concat
- join
- slice_before
- keep_if,- select!
- singleton_class
Note: The methods of Random can't be required individually; the class
can only be required whole with require 'backports/1.9.2/random'.
Additionally, the following Ruby 1.9 features have been backported:
- try_convert
- sample
- each_with_object
- each_with_index(with arguments)
- new(with block)
- binread
- to_path
- All class methods accepting filenames will accept files or anything
with a #to_pathmethod.
- File.openaccepts an options hash.
- round
- assoc,- rassoc
- key
- try_convert
- default_proc=
- magnitude
- round
- bin_read
- try_convert
- ungetbyte
- IO.openaccepts an options hash.
- require_relative
- log(with base)
- log2
- round
- define_singleton_method
- public_method
- public_send
- yield
- lambda?
- curry
- ===
- cover?
- try_convert
- ascii_only?
- chr
- clear
- codepoints,- each_codepoint
- get_byte,- set_byte
- ord
- try_convert
Enumerator can be accessed directly (instead of Enumerable::Enumerator)
To include only these backports and those of the 1.8 line, require "backports/1.9.1".
Moreover, a pretty good imitation of BasicObject is available, but since it
is only an imitation, it must be required explicitly:
require 'backports/basic_object'
Complete Ruby 1.8.7 backporting (core language). Refer to the official list of changes. That's about 130 backports!
Only exceptions:
Libraries were slowly being backported, but they are now available as separate gems.
The backports would be automatically used after requiring 'backports/std_lib' but this is now deprecated and discouraged.
The following libraries are up to date with Ruby 1.9.3:
The following library is to date with Ruby 2.0.0:
I am aware of the following backport gem, which probably won't make it into this gem:
[smtp_tls](http://seattlerb.rubyforge.org/smtp_tls/)
Requiring backports for a given version of Ruby will also load 'backports/std_lib'.
Some backports would create incompatibilities in their current Ruby version but could be useful in some projects. It is possible to request such incompatible changes. Backports currently supports the following:
- select(returns a Hash instead of an Array)
- map(returns an enumerator when called without a block)
- length,- size(for UTF-8 support)
These must be imported in addition to the backports gem, for example:
require "backports/force/hash_select"
{}.select{} # => {}, even in Ruby 1.8
Thanks for the bug reports and patches, in particular the repeat offenders:
Arto Bendiken ( bendiken )
Konstantin Haase ( rkh )
Roger Pack ( rdp )
Victor Shepelev ( zverok )
The best way to submit a patch is to also submit a patch to ruby/spec and then a patch to backports that make it pass the spec.
See below to test rubyspec. Note that only features missing from your Ruby version are tested.
git submodule init && git submodule update # => pulls rubyspecs
bundle install
bundle exec rake spec[hash/slice]          # => tests Hash#slice  (must be in Ruby 2.4 or less)
bundle exec rake spec[hash/*]              # => tests all backported Hash methods
bundle exec rake spec  (or rake spec[*/*]) # => all rubyspecs for backported methods
Failures that are acceptable are added the to tags file.
backports is released under the terms of the MIT License, see the included
LICENSE file.
Author : Marc-André Lafortune