From: Sean O'Halpin Date: 2007-04-06T23:28:20+09:00 Subject: Re: A convoluted question about stdout On 4/6/07, Sonny Chee wrote: > Hey Guys, > > I know I can do a $stdout.reopen and redirect it to a file. Is there a > more direct way to get at the contents of stdout... say, redirect stdout > to variable? > > Sonny. > > -- > Posted via http://www.ruby-forum.com/. > > Maybe something like this (a variation on Bruce's post): def capture_stdout(&block) raise ArgumentError, "No block given" if !block_given? old_stdout = $stdout $stdout = sio = StringIO.new yield $stdout = old_stdout sio.rewind sio.read end txt = capture_stdout do puts "dlroW olleH" end puts txt.reverse Regards, Sean