Sha256: 8a052dc1f5072eedd0a63fdad94cd985c81e8372a48249c319a6eb9f4a2c1649

Contents?: true

Size: 903 Bytes

Versions: 18

Compression:

Stored size: 903 Bytes

Contents

##
# Allows $stdout to be set via Thread.current[:stdout] per thread.
# By Eric Hodel, taken from http://blog.segment7.net/articles/2006/08/16/setting-stdout-per-thread

# Thread local $stdout.
module ThreadOut #:nodoc:

  ##
  # Writes to Thread.current[:stdout] instead of STDOUT if the thread local is
  # set.

  def self.write(stuff)
    if Thread.current[:stdout] then
      Thread.current[:stdout].write stuff 
    else
      STDOUT.write stuff
    end
  end
  
  def self.<<(stuff)
      self.write(stuff)
  end
  
  def self.output_to(io)
      prev_out = Thread.current[:stdout]
      if block_given?
          Thread.current[:stdout] = io
          yield
          Thread.current[:stdout] = prev_out
          io.rewind
      else
          Thread.current[:stdout] = io
          return prev_out
      end
  end
  
  def output_to(io, &proc)
      self.class.output_to(io, &proc)
  end
  
end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
spiderfw-1.0.1 lib/spiderfw/utils/thread_out.rb
spiderfw-1.0.0 lib/spiderfw/utils/thread_out.rb
spiderfw-0.6.39 lib/spiderfw/utils/thread_out.rb
spiderfw-0.6.38 lib/spiderfw/utils/thread_out.rb
spiderfw-0.6.37 lib/spiderfw/utils/thread_out.rb
spiderfw-0.6.35 lib/spiderfw/utils/thread_out.rb
spiderfw-0.6.34 lib/spiderfw/utils/thread_out.rb
spiderfw-0.6.33 lib/spiderfw/utils/thread_out.rb
spiderfw-0.6.32 lib/spiderfw/utils/thread_out.rb
spiderfw-0.6.31 lib/spiderfw/utils/thread_out.rb
spiderfw-0.6.30 lib/spiderfw/utils/thread_out.rb
spiderfw-0.6.29 lib/spiderfw/utils/thread_out.rb
spiderfw-0.6.28 lib/spiderfw/utils/thread_out.rb
spiderfw-0.6.27 lib/spiderfw/utils/thread_out.rb
spiderfw-0.6.26 lib/spiderfw/utils/thread_out.rb
spiderfw-0.6.26.pre1 lib/spiderfw/utils/thread_out.rb
spiderfw-0.6.25 lib/spiderfw/utils/thread_out.rb
spiderfw-0.6.24 lib/spiderfw/utils/thread_out.rb