Ruby 2.7 backports

This commit is contained in:
zverok 2020-01-29 19:01:38 +02:00 committed by Marc-André Lafortune
parent 6c87e4a843
commit 9b799c2023
20 changed files with 143 additions and 2 deletions

View File

@ -75,6 +75,27 @@ Compatible with Ruby itself, JRuby and Rubinius.
= Complete List of backports
== Ruby 2.7 backports
* Array
* +intersection+
* Comparable
* +clamp+ (with range)
* Complex
* +<=>+
* Enumerable
* +filter_map+
* +tally+
* Enumerator
* +produce+ (class method)
* Time
* +floor+, +ceil+
== Ruby 2.6 backports
* Array

View File

@ -27,6 +27,7 @@ class SpecRunner
puts "*** mspec returned with unexpected results:"
puts result
puts "Command was:", cmd
exit
end
_, ex, p, f, e = data = match.captures.map{|x| x.to_i}
not_found << path if ex == 0

@ -1 +1 @@
Subproject commit ebe6723386809bee096c2260d14e8f84bbc76eaf
Subproject commit f8a2d541d6e441e94fd1e01ecd03e4da2a0bcd30

3
lib/backports/2.7.0.rb Normal file
View File

@ -0,0 +1,3 @@
# require this file to load all the backports up to Ruby 2.5
require 'backports/2.6'
Backports.require_relative_dir

View File

@ -0,0 +1,3 @@
require 'backports/tools/require_relative_dir'
Backports.require_relative_dir

View File

@ -0,0 +1,5 @@
class Array
def intersection(*arrays)
arrays.inject(Array.new(self), :&)
end unless method_defined? :intersection
end

View File

@ -0,0 +1,3 @@
require 'backports/tools/require_relative_dir'
Backports.require_relative_dir

View File

@ -0,0 +1,22 @@
require 'backports/2.4.0/comparable/clamp' unless Comparable.method_defined? :clamp
if Comparable.instance_method(:clamp).parameters == [[:req], [:req]]
require 'backports/tools/alias_method_chain'
module Comparable
def clamp_with_range(range_or_min, max = nil)
return clamp_without_range(range_or_min, max) if max
raise TypeError, "wrong argument type #{range_or_min.class} (expected Range)" unless range_or_min.is_a?(Range)
if range_or_min.end.nil? # 2.6's endless range
self < range_or_min.begin ? range_or_min.begin : self
elsif range_or_min.exclude_end?
raise ArgumentError, 'cannot clamp with an exclusive range'
else
clamp_without_range(range_or_min.begin, range_or_min.end)
end
end
Backports.alias_method_chain self, :clamp, :range
end
end

View File

@ -0,0 +1,3 @@
require 'backports/tools/require_relative_dir'
Backports.require_relative_dir

View File

@ -0,0 +1,13 @@
unless Complex.method_defined?(:<=>)
class Complex
def <=>(other)
return nil unless imaginary.zero?
if other.is_a?(Complex)
other.imaginary.zero? ? real <=> other.real : nil
else
real <=> other
end
end
end
end

View File

@ -0,0 +1,3 @@
require 'backports/tools/require_relative_dir'
Backports.require_relative_dir

View File

@ -0,0 +1,12 @@
unless Enumerable.method_defined? :filter_map
module Enumerable
def filter_map
return to_enum(__method__) unless block_given?
each_with_object([]) { |item, res|
processed = yield(item)
res << processed if processed
}
end
end
end

View File

@ -0,0 +1,11 @@
unless Enumerable.method_defined? :tally
module Enumerable
def tally
# NB: By spec, we can't use Hash.new(0), because tally should return default-less hash
each_with_object({}) { |item, res|
res[item] ||= 0
res[item] += 1
}
end
end
end

View File

@ -0,0 +1,3 @@
require 'backports/tools/require_relative_dir'
Backports.require_relative_dir

View File

@ -0,0 +1,18 @@
unless Enumerator.respond_to?(:produce)
class Enumerator
NOVALUE__ = Object.new.freeze
def self.produce(initial = NOVALUE__)
raise ArgumentError, 'no block given' unless block_given?
Enumerator.new do |y|
val = initial == NOVALUE__ ? yield() : initial
loop do
y << val
val = yield(val)
end
end
end
end
end

View File

@ -0,0 +1,9 @@
unless Time.method_defined?(:ceil)
class Time
def ceil(ndigits = 0)
s = (sec + subsec)
change = s.ceil(ndigits) - s
self + change
end
end
end

View File

@ -0,0 +1,9 @@
unless Time.method_defined?(:floor)
class Time
def floor(ndigits = 0)
s = (sec + subsec)
change = s - s.floor(ndigits)
self - change
end
end
end

1
lib/backports/2.7.rb Normal file
View File

@ -0,0 +1 @@
require 'backports/2.7.0'

1
set_version/2.7.0.rb Normal file
View File

@ -0,0 +1 @@
require File.expand_path(File.dirname(__FILE__) + "/setter")

@ -1 +1 @@
Subproject commit 7de852d1bc559f021fefd81cac3bd3048b6e1877
Subproject commit f8a2d541d6e441e94fd1e01ecd03e4da2a0bcd30