From: James Edward Gray II Date: 2006-03-25T01:07:07+09:00 Subject: Re: redirecting stdout to a String On Mar 24, 2006, at 8:12 AM, Mark Volkmann wrote: > On 3/24/06, James Edward Gray II wrote: >> On Mar 24, 2006, at 7:50 AM, Mark Volkmann wrote: >> >>> I wrote a unit test for a method that writes to stdout. I got it to >>> work, but I'm not sure my approach was the best. Is there a more >>> recommended way to do this? >>> >>> # Create a String with a singleton write method >>> # that allows it to take the place of stdout. >>> actual = ''' >>> def actual.write(data); self << data; end >> >> See the standard library, StringIO. > > Thanks! I have that working, but it's not clear to me how that is > better. In fact, it seems to take more code to do it that way. You originally asked: >>> Is there a more recommended way to do this? I was just trying to answer that question. I prefer the StringIO method because it is a full mock API, as Robert Dober said. However, if you know you will only need the one method (now and forever?!) there is probably little difference. James Edward Gray II