I'm not sure I understand your question, but the code you are trying to 
write seems to work fine:

$ irb
>> result = ""
=> ""
>> result << "Hello"
=> "Hello"
>>

Is this what you want?

On Wed, Mar 26, 2003 at 09:45:21AM +0900, makotz wrote:
> Hi,
> 
> 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
>     ----------------------------------------
>     
>     ----------------------------------------
>     ##
>     ## test program
>     ##
>     require 'main'
>     require 'runit/testcase'
>     require 'runit/cui/testrunner'
>     
>     class MainTest < RUNIT::TestCase
>       def test_print_list
>         list = [10, 20, 30]
>         print_list(list)        # <== how to test?
>       end
>     end
>     
>     testcases = MainTest.test_cases()
>     RUNIT::CUI::TestRunner.run(testcases)
>     ----------------------------------------
> 
> 
> If I could change a stream of print to a string, I would write it
> like the following:
> 
>     ----------------------------------------
>       ...(snip)...
>     
>       def test_print_list
>         list = [10, 20, 30]
>         expected = <<END
>     1: 10
>     2: 20
>     3: 30
>     END
>         result = ''
>         result << print_list(list)     # if I could
>         assert_equal(expected, result)
>       end
>     
>       ...(snip)...
>     ----------------------------------------
> 
> 
> Please give me an advice.
> 
> --
> regards,
> makotz kuwata
> 

-- 
Daniel Carrera
Graduate Teaching Assistant.  Math Dept.
University of Maryland.  (301) 405-5137
--
en banc: en banc (ahn-BAHNK) adjective, adverb
   Having all the judges of a court present in a hearing.