From: Mark Volkmann Date: 2006-03-24T23:12:16+09:00 Subject: Re: redirecting stdout to a String 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. I now have this. require 'stringio' sio = StringIO.new old_stdout, $stdout = $stdout, sio # Invoke method to test that writes to stdout. $stdout = old_stdout # restore stdout actual = sio.string Am I overcomplicating this? -- R. Mark Volkmann Object Computing, Inc.