From 3901db0d75dce0b533db893714c7ec712f6068b8 Mon Sep 17 00:00:00 2001 From: Charles Oliver Nutter Date: Mon, 4 Mar 2024 22:03:38 -0600 Subject: [PATCH 1/6] A few small fixes for rubyspecs * Size error cases * Enumerator.product rejects kwargs by name * Product#rewind rewinds in "direct order", not reverse --- lib/backports/3.2.0/enumerator/product.rb | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/lib/backports/3.2.0/enumerator/product.rb b/lib/backports/3.2.0/enumerator/product.rb index 8a7b135c..bf49a09b 100644 --- a/lib/backports/3.2.0/enumerator/product.rb +++ b/lib/backports/3.2.0/enumerator/product.rb @@ -1,12 +1,19 @@ unless Enumerator.method_defined? :product if RUBY_VERSION >= '2.7' instance_eval <<-EOT, __FILE__, __LINE__ + 1 - def Enumerator.product(*enums, **nil, &block) + def Enumerator.product(*enums, **kwargs, &block) + if kwargs && !kwargs.empty? + raise ArgumentError.new("unknown keywords: " + kwargs.keys.map(&:inspect).join(", ")) + end Enumerator::Product.new(*enums).each(&block) end EOT else def Enumerator.product(*enums, &block) + kwargs = enums[-1] + if kwargs.kind_of?(Hash) && !kwargs.empty? + raise ArgumentError.new("unknown keywords: " + kwargs.keys.map(&:inspect).join(", ")) + end Enumerator::Product.new(*enums).each(&block) end end @@ -47,15 +54,17 @@ def __execute(block, values, enums) def size total_size = 1 @__enums.each do |enum| + return nil unless enum.respond_to?(:size) size = enum.size - return size if size == nil || size == Float::INFINITY + return size if size == nil || size == Float::INFINITY || size == -Float::INFINITY + return nil unless size.kind_of?(Integer) total_size *= size end total_size end def rewind - @__enums.reverse_each do |enum| + @__enums.each do |enum| enum.rewind if enum.respond_to?(:rewind) end self From 4f5b6dbe343b8be3a664a82b7f0577b2d2e28760 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc-Andr=C3=A9=20Lafortune?= Date: Tue, 5 Mar 2024 15:05:52 -0500 Subject: [PATCH 2/6] Update lib/backports/3.2.0/enumerator/product.rb Let's not go out of our way to return senseless results. See https://github.com/ruby/spec/pull/1140 --- lib/backports/3.2.0/enumerator/product.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/backports/3.2.0/enumerator/product.rb b/lib/backports/3.2.0/enumerator/product.rb index bf49a09b..408bc951 100644 --- a/lib/backports/3.2.0/enumerator/product.rb +++ b/lib/backports/3.2.0/enumerator/product.rb @@ -56,7 +56,7 @@ def size @__enums.each do |enum| return nil unless enum.respond_to?(:size) size = enum.size - return size if size == nil || size == Float::INFINITY || size == -Float::INFINITY + return size if size == nil || size == Float::INFINITY return nil unless size.kind_of?(Integer) total_size *= size end From 0189573ade768a65028d62de07bcaa834a6c8ea7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc-Andr=C3=A9=20Lafortune?= Date: Tue, 5 Mar 2024 15:16:59 -0500 Subject: [PATCH 3/6] Satisfy rubocop --- lib/backports/3.2.0/enumerator/product.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/backports/3.2.0/enumerator/product.rb b/lib/backports/3.2.0/enumerator/product.rb index 408bc951..bda52654 100644 --- a/lib/backports/3.2.0/enumerator/product.rb +++ b/lib/backports/3.2.0/enumerator/product.rb @@ -3,7 +3,7 @@ instance_eval <<-EOT, __FILE__, __LINE__ + 1 def Enumerator.product(*enums, **kwargs, &block) if kwargs && !kwargs.empty? - raise ArgumentError.new("unknown keywords: " + kwargs.keys.map(&:inspect).join(", ")) + raise ArgumentError, "unknown keywords: #{kwargs.keys.map(&:inspect).join(", ")}" end Enumerator::Product.new(*enums).each(&block) end @@ -11,7 +11,7 @@ def Enumerator.product(*enums, **kwargs, &block) else def Enumerator.product(*enums, &block) kwargs = enums[-1] - if kwargs.kind_of?(Hash) && !kwargs.empty? + if kwargs.is_a?(Hash) && !kwargs.empty? raise ArgumentError.new("unknown keywords: " + kwargs.keys.map(&:inspect).join(", ")) end Enumerator::Product.new(*enums).each(&block) @@ -57,7 +57,7 @@ def size return nil unless enum.respond_to?(:size) size = enum.size return size if size == nil || size == Float::INFINITY - return nil unless size.kind_of?(Integer) + return nil unless size.is_a?(Integer) total_size *= size end total_size From d7caa14694d3a76f1bf37fe116d58aba5f97a90e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc-Andr=C3=A9=20Lafortune?= Date: Tue, 5 Mar 2024 15:21:57 -0500 Subject: [PATCH 4/6] Satisfy rubocop --- lib/backports/3.2.0/enumerator/product.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/backports/3.2.0/enumerator/product.rb b/lib/backports/3.2.0/enumerator/product.rb index bda52654..2b2d401f 100644 --- a/lib/backports/3.2.0/enumerator/product.rb +++ b/lib/backports/3.2.0/enumerator/product.rb @@ -12,7 +12,7 @@ def Enumerator.product(*enums, **kwargs, &block) def Enumerator.product(*enums, &block) kwargs = enums[-1] if kwargs.is_a?(Hash) && !kwargs.empty? - raise ArgumentError.new("unknown keywords: " + kwargs.keys.map(&:inspect).join(", ")) + raise ArgumentError, "unknown keywords: #{kwargs.keys.map(&:inspect).join(", ")}" end Enumerator::Product.new(*enums).each(&block) end From f7d423ddf78d83d86ed08a9989cbf6d12e22af7e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc-Andr=C3=A9=20Lafortune?= Date: Tue, 5 Mar 2024 15:25:07 -0500 Subject: [PATCH 5/6] Update lib/backports/3.2.0/enumerator/product.rb --- lib/backports/3.2.0/enumerator/product.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/backports/3.2.0/enumerator/product.rb b/lib/backports/3.2.0/enumerator/product.rb index 2b2d401f..b6443de8 100644 --- a/lib/backports/3.2.0/enumerator/product.rb +++ b/lib/backports/3.2.0/enumerator/product.rb @@ -1,6 +1,6 @@ unless Enumerator.method_defined? :product if RUBY_VERSION >= '2.7' - instance_eval <<-EOT, __FILE__, __LINE__ + 1 + instance_eval <<-'EOT', __FILE__, __LINE__ + 1 def Enumerator.product(*enums, **kwargs, &block) if kwargs && !kwargs.empty? raise ArgumentError, "unknown keywords: #{kwargs.keys.map(&:inspect).join(", ")}" From 816174a8deffd78330afc362445acc06101dd69f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc-Andr=C3=A9=20Lafortune?= Date: Tue, 5 Mar 2024 15:28:33 -0500 Subject: [PATCH 6/6] Apply suggestions from code review Satisfy rubocop --- lib/backports/3.2.0/enumerator/product.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/backports/3.2.0/enumerator/product.rb b/lib/backports/3.2.0/enumerator/product.rb index b6443de8..87337ab6 100644 --- a/lib/backports/3.2.0/enumerator/product.rb +++ b/lib/backports/3.2.0/enumerator/product.rb @@ -3,7 +3,7 @@ instance_eval <<-'EOT', __FILE__, __LINE__ + 1 def Enumerator.product(*enums, **kwargs, &block) if kwargs && !kwargs.empty? - raise ArgumentError, "unknown keywords: #{kwargs.keys.map(&:inspect).join(", ")}" + raise ArgumentError, "unknown keywords: #{kwargs.keys.map(&:inspect).join(', ')}" end Enumerator::Product.new(*enums).each(&block) end @@ -12,7 +12,7 @@ def Enumerator.product(*enums, **kwargs, &block) def Enumerator.product(*enums, &block) kwargs = enums[-1] if kwargs.is_a?(Hash) && !kwargs.empty? - raise ArgumentError, "unknown keywords: #{kwargs.keys.map(&:inspect).join(", ")}" + raise ArgumentError, "unknown keywords: #{kwargs.keys.map(&:inspect).join(', ')}" end Enumerator::Product.new(*enums).each(&block) end