Skip to content

Commit

Permalink
Fix Enumerator.product.size (#192)
Browse files Browse the repository at this point in the history
* Size error cases
* Enumerator.product rejects kwargs by name
* Product#rewind rewinds in "direct order", not reverse

Co-authored-by: Marc-André Lafortune <github@marc-andre.ca>
  • Loading branch information
headius and marcandre committed Mar 5, 2024
1 parent 94a18c5 commit 83904f5
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions lib/backports/3.2.0/enumerator/product.rb
Original file line number Diff line number Diff line change
@@ -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)
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(', ')}"
end
Enumerator::Product.new(*enums).each(&block)
end
EOT
else
def Enumerator.product(*enums, &block)
kwargs = enums[-1]
if kwargs.is_a?(Hash) && !kwargs.empty?
raise ArgumentError, "unknown keywords: #{kwargs.keys.map(&:inspect).join(', ')}"
end
Enumerator::Product.new(*enums).each(&block)
end
end
Expand Down Expand Up @@ -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 nil unless size.is_a?(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
Expand Down

0 comments on commit 83904f5

Please sign in to comment.