Created
March 2, 2012 16:08
-
-
Save nefo-mi/1959364 to your computer and use it in GitHub Desktop.
Rubyで標準出力をナニしたい時に書いたコード ref: http://qiita.com/items/2951
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
class Display | |
attr_accessor :print | |
def initialize | |
@print = [] | |
end | |
def write(msg) | |
@print.push(msg) | |
end | |
end | |
def display_capture | |
disp = Display.new | |
$stdout = disp | |
yield | |
$stdout = STDOUT | |
return disp.print | |
end | |
def test_hoge | |
res = display_capture do | |
print 1 | |
print 2 | |
end | |
assert_equal([1, 2], res) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment