Created
September 25, 2009 15:25
-
-
Save kirel/193617 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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