Hi,

In message "[ruby-talk:15501] StringBuffer IO Class"
    on 01/05/22, "Wayne Blair" <wayne.blair / relian.com> writes:

|Here is pseudocode for what I want to do - I'd be really appreciative if
|anyone could help fill in the blanks with ruby code.
|
|def foo
|  # redirect stdout to a buffer (buff)
|
|  # do some work that may result in writes to stdout
|
|  # redirect stdout back to original stdout
|
|  # write contents of buff to stdout
|end

Here is realcode for what you want to do. ;-)

  def foo
    # redirect stdout to a buffer (buff)
    buff = ""
    def buff.write(str)
      self << str
    end
    $defout = buff

    # do some work that may result in writes to stdout
    print "hello world\n"

    # redirect stdout back to original stdout
    $defout = STDOUT

    # write contents of buff to stdout
    print buff
  end

"write" method is all you need for redirection.

							matz.