From: ptkwt@...1.aracnet.com (Phil Tomson) Date: 2002-11-15T17:02:50+09:00 Subject: Re: Howot redirect STDOUT to a string within a script? In article <41-Fri15Nov2002124848+0900-eban@os.rim.or.jp>, WATANABE Hirofumi wrote: >Phil Tomson writes: > >> Let's say I've got some strings with Ruby code to be eval'ed, like: >> >> evalstr = 'puts "this is the string"' >> >> and let's say I want to get the part that's sent to STDOUT (by puts) and >> capture it in a string, like so: >> >> outputStr = eval evalstr #=> outputStr = "this is the string" >> >> I suspect I could do this by redefining 'puts', but is there a way of >> doing it so that puts is only redefined within the context of the eval? > >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 I have ruby 1.7 from CVS but the require 'stringio' failed. Where is 1.7's stringio? Phil