Issue #6824 has been updated by mame (Yusuke Endoh).

Status changed from Open to Rejected
Assignee changed from mame (Yusuke Endoh) to matz (Yukihiro Matsumoto)
Target version changed from 2.0.0 to next minor

Hello,

jasiek (Jan Szumiec) wrote:
> This is useful when you're merging multiple collections using min/max - when one of the collections is exhausted it can be easily ignored.

You are misunderstanding merge sort.
Your example does never work as intended.

>   fail unless merged != merged.sort

I bet this condition is inverted.

The proposal itself might be useful, but this ticket is based on misconception.
So I'm closing it.  Please open another one if you wish.

-- 
Yusuke Endoh <mame / tsg.ne.jp>
----------------------------------------
Feature #6824: StopIteration gets a source method
https://bugs.ruby-lang.org/issues/6824#change-31773

Author: jasiek (Jan Szumiec)
Status: Rejected
Priority: Normal
Assignee: matz (Yukihiro Matsumoto)
Category: core
Target version: next minor


=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
    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
      enumerators.map(&:peek)
      values = enumerators.map(&:next)

      merged += values.sort
    rescue StopIteration => e
      enumerators.delete(e.source)
      retry
    end
  end

  fail unless merged != merged.sort

Attached is a patch against trunk.
=end




-- 
http://bugs.ruby-lang.org/