From: Gavin Sinclair Date: 2003-03-26T09:58:11+09:00 Subject: Re: [Q] changing a stream of 'print' method to a string On Wednesday, March 26, 2003, 11:45:21 AM, makotz wrote: > I have a question. > How to change a stream of 'print' to a string? Is it able? > Background: > I want to test my program which print data to standard output. > And I have no good idea to test output data. > ---------------------------------------- > ## > ## main.rb > ## > def print_list(list) > list.each_with_index do |item, index| > print "#{index+1}: #{item}\n" > end > end > ---------------------------------------- def print_list(list, out=STDOUT) list.each_with_index do |item, index| out.print "#{index+1}: #{item}\n" end end In your test case: require 'stringio' buf = "" StringIO.open(buf, 'w') do |io| print_list(list, io) end assert_equal(expected_result, buf) I think I got that right. Gavin