class SlowEnumeratorTools::Merger::Iterator

Public Class Methods

new(enums) click to toggle source
# File lib/slow_enumerator_tools/merger.rb, line 14
def initialize(enums)
  @enums = enums
  @q = SizedQueue.new(5)
  @done = false
end

Public Instance Methods

next() click to toggle source
# File lib/slow_enumerator_tools/merger.rb, line 20
def next
  raise StopIteration if @done

  nxt = @q.pop
  if SlowEnumeratorTools::Util::STOP_OK.equal?(nxt)
    @done = true
    raise StopIteration
  elsif SlowEnumeratorTools::Util::STOP_ERR.equal?(nxt)
    raise @q.pop
  else
    nxt
  end
end
start() click to toggle source
# File lib/slow_enumerator_tools/merger.rb, line 34
def start
  threads = @enums.map { |enum| spawn_empty_into(enum, @q) }

  Thread.new do
    threads.each(&:join)
    @q << SlowEnumeratorTools::Util::STOP_OK
  end
end

Protected Instance Methods

spawn_empty_into(enum, queue) click to toggle source
# File lib/slow_enumerator_tools/merger.rb, line 45
def spawn_empty_into(enum, queue)
  Thread.new do
    begin
      enum.each { |e| queue << e }
    rescue StandardError => e
      queue << SlowEnumeratorTools::Util::STOP_ERR
      queue << e
    end
  end
end