1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713
|
# Backports Library [<img src="https://travis-ci.org/marcandre/backports.svg?branch=master">](https://travis-ci.org/marcandre/backports) [<img src="https://badge.fury.io/rb/backports.svg" alt="Gem Version" />](http://badge.fury.io/rb/backports) [](https://tidelift.com/subscription/pkg/rubygems-backports?utm_source=rubygems-backports&utm_medium=referral&utm_campaign=readme)
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.
## Loading backports
### Explicitly (recommended)
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.
### By Module
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.
### Up to a specific Ruby version (for quick coding)
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.
## What's inside
Goals for backported features:
1. Won't break older code
2. Pure Ruby (no C extensions)
3. Pass [ruby/spec](https://github.com/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...
[an example from rails]: https://github.com/rails/rails/blob/a4b55827721a5967299f3c1531afb3d6d81e4ac0/activerecord/lib/active_record/associations/association.rb#L155-L159
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).
## Installation & compatibility
`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.
# Complete List of backports
## Ruby 3.3 backports
#### MatchData
- `named_captures` (with `symbolize_keys` option)
#### Range
- `reverse_each` (with correct handling of endless Ranges)
- `overlap?`
## Ruby 3.2 backports
#### Class
- `attached_object`
#### Data
- Complete class
#### Enumerator
- `Enumerator.product` and `Enumerator::Product`
#### Hash
- `shift` (with correct behavior when empty)
#### Integer
- `ceildiv`
#### MatchData
- `byteoffset`
## Ruby 3.1 backports
#### Array
- `intersect?`
#### Class
- `subclasses`
#### Enumerable
- `compact`
- `tally` (with hash argument)
### File
- `dirname` (with depth argument)
#### Integer
- `try_convert`
#### MatchData
- `match`
- `match_length`
#### Struct
- `keyword_init?`
## Ruby 3.0 backports
#### Env
- `except`
#### Hash
- `except`
- `transform_keys`, `transform_keys!` (with hash argument)
#### Ractor
- All methods, with the caveats:
- uses Ruby's `Thread` internally
- will not raise some errors when `Ractor` would (in particular `Ractor::IsolationError`)
- supported in Ruby 2.0+ only
#### Symbol
- `name`
## Ruby 2.7 backports
#### Array
- `intersection`
#### Comparable
- `clamp` (with range)
#### Complex
- `<=>`
#### Enumerable
- `filter_map`
- `tally`
#### Enumerator
- `produce` (class method)
#### Symbol
- `end_with?`
- `start_with?`
#### Time
- `floor`, `ceil`
## Ruby 2.6 backports
#### Array
- `difference`, `union`
- `to_h` (with block)
#### Enumerable
- `chain`
- `to_h` (with block)
#### Enumerator::Chain (use Enumerable#chain)
#### Hash
- `merge`, `merge!`/`update` (with multiple arguments)
- `to_h` (with block)
#### Kernel
- `then`
#### Method
- `<<`, `>>`
#### Proc
- `<<`, `>>`
#### Range
- `cover?` (with `Range` argument)
## Ruby 2.5 backports
#### Array
- `append`, `prepend`
#### Dir
- `children`, `each_child`
#### Enumerable
- `any?`, `all?`, `none?`, `one?` (with pattern argument)
#### Hash
- `slice`
- `transform_keys`
#### Integer
- `sqrt`
- `allbits?`, `anybits?` and `nobits?`
#### Kernel
- `yield_self`
#### Module
- `attr`, `attr_accessor`, `attr_reader`, `attr_writer` (now public)
- `define_method`, `alias_method`, `undef_method`, `remove_method` (now
public)
#### String
- `delete_prefix`, `delete_prefix!`
- `delete_suffix`, `delete_suffix!`
- `undump`
#### Struct
- `new` (with `keyword_init: true`)
## Ruby 2.4 backports
#### Comparable
- `clamp`
#### Enumerable
- `sum`
- `uniq`
#### Hash
- `compact`, `compact!`
- `transform_values`, `transform_values!`
#### Queue
- `close`, `closed?`
#### Regexp
- `match?`
#### String
- `match?`
- `unpack1`
#### FalseClass, Fixnum, Bignum, Float, NilClass, TrueClass
- `dup`
## Ruby 2.3 backports
#### Array
- `bsearch_index`
- `dig`
#### Enumerable
- `chunk_while`
- `grep_v`
#### Hash
- `dig`
- `fetch_values`
- `to_proc`
- <=, <, >=, >
#### Numeric
- `negative?`
- `positive?`
#### String
- unary + and -
#### Struct
- `dig`
## Ruby 2.2 backports
#### Enumerable
- `slice_after`
- `slice_when`
#### Float
- `prev_float`
- `next_float`
#### Kernel
- `itself`
#### Method
- `curry`
- `super_method`
#### String
- `unicode_normalize`
- `unicode_normalize!`
- `unicode_normalize?`
## Ruby 2.1 backports
#### Array
- `to_h`
#### Bignum
- `bit_length`
#### Enumerable
- `to_h`
#### Fixnum
- `bit_length`
#### Module
- `include` (now public)
- `singleton_class?`
## Ruby 2.0 backports
#### Array
- `bsearch`
#### Enumerable
- `lazy`
#### Enumerator::Lazy
- all methods
#### Hash
- `default_proc=` (with nil argument)
- `to_h`
#### `nil.to_h`
#### Range
- `bsearch`
#### Struct
- `to_h`
## Ruby 1.9.3 backports
#### File
- `NULL`
#### IO
- `advise` (acts as a noop)
- `write`, `binwrite`
#### String
- `byteslice`
- `prepend`
## Ruby 1.9.2 backports
#### Array
- `rotate, rotate!`
- `keep_if, select!`
- `product` (with block)
- `repeated_combination`, `repeated_permutation`
- `sort_by!`
- `uniq, uniq!` (with block)
#### Complex
- `to_r`
#### Dir
- `home`
#### Enumerable
- `chunk`
- `flat_map`, `collect_concat`
- `join`
- `slice_before`
#### Float::INFINITY, NAN
#### Hash
- `keep_if`, `select!`
#### Object
- `singleton_class`
#### Random (new 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'`.
## Ruby 1.9.1 backports
Additionally, the following Ruby 1.9 features have been backported:
#### Array
- `try_convert`
- `sample`
#### Enumerable
- `each_with_object`
- `each_with_index` (with arguments)
#### Enumerator
- `new` (with block)
#### File
- `binread`
- `to_path`
- All class methods accepting filenames will accept files or anything
with a `#to_path` method.
- `File.open` accepts an options hash.
#### Float
- `round`
#### Hash
- `assoc`, `rassoc`
- `key`
- `try_convert`
- `default_proc=`
#### Integer
- `magnitude`
- `round`
#### IO
- `bin_read`
- `try_convert`
- `ungetbyte`
- `IO.open` accepts an options hash.
#### Kernel
- `require_relative`
#### Math
- `log` (with base)
- `log2`
#### Numeric
- `round`
#### Object
- `define_singleton_method`
- `public_method`
- `public_send`
#### Proc
- `yield`
- `lambda?`
- `curry`
- `===`
#### Range
- `cover?`
#### Regexp
- `try_convert`
#### String
- `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'
## Ruby 1.8.7
Complete Ruby 1.8.7 backporting (core language). Refer to the official list of
[changes](https://github.com/ruby/ruby/blob/ruby_1_8_7/NEWS). That's about 130
backports!
Only exceptions:
#### String#gsub (the form returning an enumerator)
#### GC.stress= (not implemented)
#### Array#choice (removed in 1.9, use 1.9.1's Array#sample instead)
## Libraries
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:
#### Matrix
#### Prime
#### Set
The following library is to date with Ruby 2.0.0:
#### OpenStruct (ostruct)
I am aware of the following backport gem, which probably won't make it into
this gem:
#### Net::SMTP for Ruby 1.8.6:
[smtp_tls](http://seattlerb.rubyforge.org/smtp_tls/)
Requiring backports for a given version of Ruby will also load
'backports/std_lib'.
## Forcing incompatibilities
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:
#### Hash
- `select` (returns a Hash instead of an Array)
#### Enumerable / Array
- `map` (returns an enumerator when called without a block)
#### String
- `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
Thanks for the bug reports and patches, in particular the repeat offenders:
#### Arto Bendiken ( [bendiken](http://github.com/bendiken) )
#### Konstantin Haase ( [rkh](https://github.com/rkh) )
#### Roger Pack ( [rdp](http://github.com/rdp) )
#### Victor Shepelev ( [zverok](http://github.com/zverok) )
## Contributing
The best way to submit a patch is to also submit a patch to
[ruby/spec](https://github.com/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.
# License
`backports` is released under the terms of the MIT License, see the included
LICENSE file.
Author
: Marc-André Lafortune
|