Created
April 1, 2009 18:02
-
-
Save thinkerbot/88808 to your computer and use it in GitHub Desktop.
Danger of IO duplicate
This file contains 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
# This illustrates how IO duplicates flush separately. | |
# The danger is that this can result in disordered | |
# output. Run this from the command line: | |
# | |
# % ruby io_duplicate_danger.rb | |
# 1 | |
# 2 | |
# 3 | |
# | |
# You get what you expect. Now try with redirection: | |
# | |
# % ruby io_duplicate_danger.rb &> output.txt | |
# % cat output.txt | |
# 2 | |
# 1 | |
# 3 | |
# | |
a = $stdout | |
b = a.dup | |
a.puts 1 | |
b.puts 2 | |
a.puts 3 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment