From: Idan Sofer Date: 2002-11-15T17:34:46+09:00 Subject: Re: Howot redirect STDOUT to a string within a script? On ��� ����, 15 ������� 2002, 05:48, WATANABE Hirofumi wrote: > Ruby 1.7 has StringIO. > > require 'stringio' > > def redirect > orig_defout = $defout > $defout = StringIO.new > yield > $defout.string > ensure > $defout = orig_defout > end > > evalstr = 'puts "this is the string"' > outputStr = redirect { eval evalstr } > p outputStr The problem with this approach is that it's not thread safe($defout is tampered), and that the same can be done with much less coding using instance_eval method: === require 'stringio' s=StringIO.new evalstr ='puts "this is the string"' s.instance_eval evalstr p s ===