Sha256: c486c6bb83538b7875ffaca8edd294a10491b3b7b396b653802f8cbcd31abc90

Contents?: true

Size: 843 Bytes

Versions: 1

Compression:

Stored size: 843 Bytes

Contents

# Overrides $stdout and allows it to be overridden by the thread-local variable Thread.current[:stdout].
#
# If Thread.current[:stdout] is set, the thread will write all output there. Otherwise the thread will
# use $stdout as normal.
#
# Example usage:
#
# out = StringIO.new
#
# Thread.start do
#   Thread.current[:stdout] = out
#   puts "thread1"
# end.join
#
# Thread.start do
#   puts "thread2"
# end.join
#
# puts out.string
#
# Output:
#   thread2
#   thread1

class ThreadLocalIO < IO
  @@former_stdout = $stdout

  def self.write(*args)
    if Thread.current[:stdout]
      Thread.current[:stdout].write(*args)
    else
      @@former_stdout.write(*args)
    end
  end

  def self.flush
    if Thread.current[:stdout]
      Thread.current[:stdout].flush
    else
      @@former_stdout.flush
    end
  end
end

$stdout = ThreadLocalIO

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
fezzik-0.7.3 lib/fezzik/thread_local_io.rb