Project

General

Profile

Feature #6824

Updated by nobu (Nobuyoshi Nakada) about 7 years ago

=begin
I'd like to add a method to StopIteration that returns an instance of the enumerator which triggered this exception. Eg:



enum = [].each
begin

begin
enum.next

rescue StopIteration => ex

puts "same" if ex.source == enum

end

This is useful when you're merging multiple collections using min/max - when one of the collections is exhausted it can be easily ignored.



enumerators = 100.times.map do

rand(100).times.map do

rand

end.sort.each

end



merged = []

while !enumerators.empty?
begin

begin
enumerators.map(&:peek)

values = enumerators.map(&:next)



merged += values.sort

rescue StopIteration => e

enumerators.delete(e.source)

retry
end

end


end

fail unless merged != merged.sort

Attached is a patch against trunk.
=end


Back