Skip to content

Instantly share code, notes, and snippets.

@kirel
Created September 25, 2009 15:25
Show Gist options
  • Select an option

  • Save kirel/193617 to your computer and use it in GitHub Desktop.

Select an option

Save kirel/193617 to your computer and use it in GitHub Desktop.
module RedirectPuts
class Interceptor
def initialize io
@io = io
end
private
def puts stuff
@io.puts stuff
end
end
def redirect_puts io, &block
Interceptor.new(io).instance_eval &block
end
end
include RedirectPuts
require 'stringio'
io = StringIO.new
i = 3
puts '1'
redirect_puts(io) do
puts i
end
puts '2'
io.rewind
puts io.read
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment