Module: StdioTrap
- Defined in:
- lib/stdiotrap.rb,
lib/stdiotrap/version.rb
Constant Summary collapse
- VERSION =
"1.0.2"
Class Method Summary collapse
- .capture(stdin = '') ⇒ Object
- .release!(force = false) ⇒ Object
- .status ⇒ Object
- .stderr ⇒ Object
- .stdout ⇒ Object
- .to_h ⇒ Object
- .trap!(stdin = '', mode = :trap) ⇒ Object
Class Method Details
.capture(stdin = '') ⇒ Object
26 27 28 29 30 31 32 33 34 35 |
# File 'lib/stdiotrap.rb', line 26 def capture(stdin='') begin require 'stringio' trap!(stdin, :capture) yield {:stdout => $stdout.string, :stderr => $stderr.string} ensure release!(true) end end |
.release!(force = false) ⇒ Object
14 15 16 17 18 19 20 |
# File 'lib/stdiotrap.rb', line 14 def release!(force=false) raise RuntimeError, "Stdio is not currently trapped" if $stdio_trap.nil? raise RuntimeError, "Can not release inside capture{}" if $stdio_trap == :capture and !force r = to_h $stdio_trap, $stdin, $stdout, $stderr = nil, $o_stdin, $o_stdout, $o_stderr r end |
.status ⇒ Object
22 23 24 |
# File 'lib/stdiotrap.rb', line 22 def status !$stdio_trap.nil? end |
.stderr ⇒ Object
42 43 44 45 |
# File 'lib/stdiotrap.rb', line 42 def stderr raise RuntimeError, "Stdio is not currently trapped" if $stdio_trap.nil? $stderr.string end |
.stdout ⇒ Object
37 38 39 40 |
# File 'lib/stdiotrap.rb', line 37 def stdout raise RuntimeError, "Stdio is not currently trapped" if $stdio_trap.nil? $stdout.string end |
.to_h ⇒ Object
47 48 49 50 |
# File 'lib/stdiotrap.rb', line 47 def to_h raise RuntimeError, "Stdio is not currently trapped" if $stdio_trap.nil? { :stdout => $stdout.string, :stderr => $stderr.string } end |
.trap!(stdin = '', mode = :trap) ⇒ Object
8 9 10 11 12 |
# File 'lib/stdiotrap.rb', line 8 def trap!(stdin='', mode=:trap) raise RuntimeError, "Stdio is already trapped" unless $stdio_trap.nil? $stdio_trap, $o_stdin, $o_stdout, $o_stderr = mode, $stdin, $stdout, $stderr $stdin, $stdout, $stderr = StringIO.new(stdin), StringIO.new, StringIO.new end |