The latest features of Ruby backported to older versions.
The latest features of Ruby backported to older versions.
Yearning to write a gem using some new cool features in Ruby 3.3 while
still supporting Ruby 2.5.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.
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, to_proc and comparisons.
You can load all backports up to a specific version. For example, to bring any version of Ruby mostly up to Ruby 3.3.0's standards:
require 'backports/3.3.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 3.3.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:
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 false
Run bundle install and require the desired backports. Compatible with Ruby
itself, JRuby and Rubinius.
named_captures (with symbolize_keys option)reverse_each (with correct handling of endless Ranges)overlap?attached_objectEnumerator.product and Enumerator::Productshift (with correct behavior when empty)ceildivbyteoffsetintersect?subclassescompacttally (with hash argument)dirname (with depth argument)try_convertmatchmatch_lengthkeyword_init?exceptexcepttransform_keys, transform_keys! (with hash argument)Thread internallyRactor would (in particular Ractor::IsolationError)nameintersectionclamp (with range)filter_maptallyproduce (class method)end_with?start_with?floor, ceildifference, unionto_h (with block)chainto_h (with block)merge, merge!/update (with multiple arguments)to_h (with block)then>>cover? (with Range argument)append, prependchildren, each_childany?, all?, none?, one? (with pattern argument)slicetransform_keyssqrtallbits?, anybits? and nobits?yield_selfattr, 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!undumpnew (with keyword_init: true)clampsumuniqcompact, compact!transform_values, transform_values!close, closed?match?match?unpack1dupbsearch_indexdigchunk_whilegrep_vdigfetch_valuesto_procnegative?positive?digslice_afterslice_whenprev_floatnext_floatitselfcurrysuper_methodunicode_normalizeunicode_normalize!unicode_normalize?to_hbit_lengthto_hbit_lengthinclude (now public)singleton_class?bsearchlazydefault_proc= (with nil argument)to_hnil.to_hbsearchto_hNULLadvise (acts as a noop)write, binwritebytesliceprependrotate, rotate!keep_if, select!product (with block)repeated_combination, repeated_permutationsort_by!uniq, uniq! (with block)to_rhomechunkflat_map, collect_concatjoinslice_beforekeep_if, select!singleton_classNote: 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_convertsampleeach_with_objecteach_with_index (with arguments)new (with block)binreadto_path#to_path method.File.open accepts an options hash.roundassoc, rassockeytry_convertdefault_proc=magnituderoundbin_readtry_convertungetbyteIO.open accepts an options hash.require_relativelog (with base)log2rounddefine_singleton_methodpublic_methodpublic_sendyieldlambda?curry===cover?try_convertascii_only?chrclearcodepoints, each_codepointget_byte, set_byteordtry_convertEnumerator 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.8Thanks for the bug reports and patches, in particular the repeat offenders:
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 methodsFailures 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
No open issues yet, or sync has not completed.