最近毎日のように読んでいるwycatsさんが便利なメソッドを利用していたのでメモしておく。 # -*- coding:utf-8 -*- require "rubygems" require "spec" # this method is written by wycats def capture(stream) begin stream = stream.to_s eval "$#{stream} = StringIO.new" yield result = eval("$#{stream}").string ensure eval("$#{stream} = #{stream.upcase}") end result end describe "Object#capture" do it do capture(:stdout) { print "hoge" }.should == "h