class ThreadedPipe

Constants

VERSION

Public Class Methods

new(*procs) click to toggle source
# File lib/threaded_pipe.rb, line 4
def initialize(*procs)
  pipes = build_pipes(procs.size - 1)

  @threads = procs.map.with_index do |procedure, index|
    Thread.new(*pipes[index]) do |*pipes|
      procedure.call(*pipes)
    ensure
      pipes.each(&:close)
    end
  end
end

Public Instance Methods

join() click to toggle source
# File lib/threaded_pipe.rb, line 16
def join
  @threads.each(&:join)
end
values() click to toggle source
# File lib/threaded_pipe.rb, line 20
def values
  @threads.map(&:value)
end

Private Instance Methods

build_pipes(count) click to toggle source
# File lib/threaded_pipe.rb, line 26
def build_pipes(count)
  pipes = [[]]

  count.times do |index|
    rd, wr = IO.pipe
    pipes[index] << wr
    pipes[index + 1] = [rd]
  end

  pipes
end