basics/stdout_redirection.rb

tests/_examples/basics/stdout_redirection.rb in the metorex repository.

class Capture
  def initialize
    @written = ""
  end

  def write(text)
    @written += text.to_s
  end

  # Ruby hands `puts` to the stream itself, so a stand-in stream answers it.
  def puts(*values)
    return write "\n" if values.empty?
    values.each { |value| write value.to_s + "\n" }
    nil
  end

  def written
    @written
  end
end

capture = Capture.new
original = $stdout
$stdout = capture

puts "through puts"
print "through print"
p "through p"
puts

$stdout = original
puts capture.written.inspect

puts :symbol
print :symbol
puts
p :symbol

$_ = "last line"
print
puts

class Speaker
  def to_s
    "speaker to_s"
  end

  def describe
    to_s
  end
end

puts Speaker.new.describe

Output:

"through puts\nthrough print\"through p\"\n\n"
symbol
symbol
:symbol
last line
speaker to_s