File: capture-output.rb

package info (click to toggle)
ruby-capture-output 1.0.0-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 128 kB
  • sloc: ruby: 59; makefile: 2
file content (30 lines) | stat: -rw-r--r-- 363 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
module Capture
	def self.output(out = STDOUT)
		r, w = IO.pipe
		old_out = out.clone
		out.reopen(w)
		data = ''

		t = Thread.new do
			data << r.read
		end

		begin
			yield
		ensure
			w.close
			out.reopen(old_out)
		end
		t.join
		data
	end

	def self.stdout(&block)
		output(STDOUT, &block)
	end

	def self.stderr(&block)
		output(STDERR, &block)
	end
end