-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
use new versions of ruby_parser and ruby2ruby
- Loading branch information
Showing
52 changed files
with
166 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
module Wrong | ||
module Capturing | ||
|
||
# Usage: | ||
# capturing { puts "hi" } => "hi\n" | ||
# capturing(:stderr) { $stderr.puts "hi" } => "hi\n" | ||
# out, err = capturing(:stdout, :stderr) { ... } | ||
# | ||
# see http://www.justskins.com/forums/closing-stderr-105096.html for more explanation | ||
def capturing(*streams) | ||
streams = [:stdout] if streams.empty? | ||
original = {} | ||
captured = {} | ||
|
||
# reassign the $ variable (which is used by well-behaved code e.g. puts) | ||
streams.each do |stream| | ||
original[stream] = (stream == :stdout ? $stdout : $stderr) | ||
captured[stream] = StringIO.new | ||
reassign_stream(stream, captured) | ||
end | ||
|
||
yield | ||
|
||
# return either one string, or an array of two strings | ||
if streams.size == 1 | ||
captured[streams.first].string | ||
else | ||
[captured[streams[0]].string, captured[streams[1]].string] | ||
end | ||
|
||
ensure | ||
|
||
streams.each do |stream| | ||
# bail if stream was reassigned inside the block | ||
if (stream == :stdout ? $stdout : $stderr) != captured[stream] | ||
raise "#{stream} was reassigned while being captured" | ||
end | ||
# support nested calls to capturing | ||
original[stream] << captured[stream].string if original[stream].is_a? StringIO | ||
reassign_stream(stream, original) | ||
end | ||
end | ||
|
||
private | ||
def reassign_stream(which, streams) | ||
case which | ||
when :stdout | ||
$stdout = streams[which] | ||
when :stderr | ||
$stderr = streams[which] | ||
end | ||
end | ||
|
||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.