Class: Fairy::Stdout
- Inherits:
-
Object
- Object
- Fairy::Stdout
- Defined in:
- lib/fairy/share/stdout.rb
Instance Attribute Summary collapse
-
#local_stdout ⇒ Object
readonly
Returns the value of attribute local_stdout.
Instance Method Summary collapse
- #flush ⇒ Object
-
#initialize(peer) ⇒ Stdout
constructor
A new instance of Stdout.
- #replace_stdout(&block) ⇒ Object
- #write(str) ⇒ Object
Constructor Details
#initialize(peer) ⇒ Stdout
Returns a new instance of Stdout.
11 12 13 14 15 16 17 18 |
# File 'lib/fairy/share/stdout.rb', line 11 def initialize(peer) @local_stdout = $stdout @peer = peer @threads = {} @mutex = Mutex.new end |
Instance Attribute Details
#local_stdout ⇒ Object (readonly)
Returns the value of attribute local_stdout.
20 21 22 |
# File 'lib/fairy/share/stdout.rb', line 20 def local_stdout @local_stdout end |
Instance Method Details
#flush ⇒ Object
47 48 49 |
# File 'lib/fairy/share/stdout.rb', line 47 def flush # do nothing end |
#replace_stdout(&block) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/fairy/share/stdout.rb', line 32 def replace_stdout(&block) @mutex.synchronize do @threads[Thread.current] = 0 unless @threads[Thread.current] @threads[Thread.current] += 1 end begin yield ensure @mutex.synchronize do @threads[Thread.current] -= 1 @threads.delete(Thread.current) if @threads[Thread.current] == 0 end end end |
#write(str) ⇒ Object
22 23 24 25 26 27 28 29 30 |
# File 'lib/fairy/share/stdout.rb', line 22 def write(str) @mutex.synchronize do if @threads[Thread.current] @peer.stdout_write(str) else @local_stdout.write(str) end end end |