Class: Ruptr::CaptureOutput
- Inherits:
-
Delegator
- Object
- Delegator
- Ruptr::CaptureOutput
- Includes:
- ForkHandler
- Defined in:
- lib/ruptr/capture_output.rb
Defined Under Namespace
Modules: ForkHandler
Instance Attribute Summary collapse
-
#real_io ⇒ Object
readonly
Returns the value of attribute real_io.
Class Method Summary collapse
- .capture_output ⇒ Object
- .fixed_install! ⇒ Object
- .install! ⇒ Object
- .installed? ⇒ Boolean
- .reset! ⇒ Object
- .uninstall! ⇒ Object
Instance Method Summary collapse
- #capture ⇒ Object
-
#initialize(real_io, tls_key) ⇒ CaptureOutput
constructor
A new instance of CaptureOutput.
Methods included from ForkHandler
Constructor Details
#initialize(real_io, tls_key) ⇒ CaptureOutput
Returns a new instance of CaptureOutput.
8 9 10 11 |
# File 'lib/ruptr/capture_output.rb', line 8 def initialize(real_io, tls_key) @real_io = real_io @tls_key = tls_key end |
Instance Attribute Details
#real_io ⇒ Object (readonly)
Returns the value of attribute real_io.
13 14 15 |
# File 'lib/ruptr/capture_output.rb', line 13 def real_io @real_io end |
Class Method Details
.capture_output ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/ruptr/capture_output.rb', line 74 def capture_output(&) unless @fixed if (ractor = defined?(::Ractor) && Ractor.current != Ractor.main) install! unless installed? else @mutex.synchronize do install! unless @pinned.positive? @pinned += 1 end end end begin stdout = stderr = nil stderr = $stderr.capture { stdout = $stdout.capture(&) } [stdout, stderr] ensure unless @fixed || ractor @mutex.synchronize do @pinned -= 1 uninstall! unless @pinned.positive? end end end end |
.fixed_install! ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/ruptr/capture_output.rb', line 61 def fixed_install! return block_given? ? yield : nil if @fixed @fixed = true install! return unless block_given? begin yield ensure @fixed = false uninstall! end end |
.install! ⇒ Object
45 46 47 48 |
# File 'lib/ruptr/capture_output.rb', line 45 def install! $stdout = new($stdout, :ruptr_stdout) $stderr = new($stderr, :ruptr_stderr) end |
.installed? ⇒ Boolean
43 |
# File 'lib/ruptr/capture_output.rb', line 43 def installed? = $stdout.is_a?(self) && $stderr.is_a?(self) |
.reset! ⇒ Object
55 56 57 58 59 |
# File 'lib/ruptr/capture_output.rb', line 55 def reset! uninstall! if @fixed || @pinned.positive? @fixed = false @pinned = 0 end |
.uninstall! ⇒ Object
50 51 52 53 |
# File 'lib/ruptr/capture_output.rb', line 50 def uninstall! $stdout = $stdout.real_io $stderr = $stderr.real_io end |
Instance Method Details
#capture ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/ruptr/capture_output.rb', line 25 def capture strio = StringIO.new(+'', 'w') saved = tls_obj[@tls_key] begin tls_obj[@tls_key] = strio yield strio.string ensure tls_obj[@tls_key] = saved strio.close end end |