Module: ThreadOut

Defined in:
lib/spiderfw/utils/thread_out.rb

Overview

Thread local $stdout.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.<<(stuff) ⇒ Object



20
21
22
# File 'lib/spiderfw/utils/thread_out.rb', line 20

def self.<<(stuff)
    self.write(stuff)
end

.output_to(io) ⇒ Object



24
25
26
27
28
29
30
31
32
33
# File 'lib/spiderfw/utils/thread_out.rb', line 24

def self.output_to(io)
    if block_given?
        prev_out = Thread.current[:stdout]
        Thread.current[:stdout] = io
        yield
        Thread.current[:stdout] = prev_out
    else
        Thread.current[:stdout] = io
    end
end

.write(stuff) ⇒ Object

Writes to Thread.current instead of STDOUT if the thread local is set.



12
13
14
15
16
17
18
# File 'lib/spiderfw/utils/thread_out.rb', line 12

def self.write(stuff)
  if Thread.current[:stdout] then
    Thread.current[:stdout].write stuff 
  else
    STDOUT.write stuff
  end
end

Instance Method Details

#output_to(io, &proc) ⇒ Object



35
36
37
# File 'lib/spiderfw/utils/thread_out.rb', line 35

def output_to(io, &proc)
    self.class.output_to(io, &proc)
end