Skip to content

Instantly share code, notes, and snippets.

@nefo-mi
Created March 2, 2012 16:08
Show Gist options
  • Save nefo-mi/1959364 to your computer and use it in GitHub Desktop.
Save nefo-mi/1959364 to your computer and use it in GitHub Desktop.
Rubyで標準出力をナニしたい時に書いたコード ref: http://qiita.com/items/2951
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