Skip to content

Instantly share code, notes, and snippets.

@thinkerbot
Created April 1, 2009 18:02
Show Gist options
  • Save thinkerbot/88808 to your computer and use it in GitHub Desktop.
Save thinkerbot/88808 to your computer and use it in GitHub Desktop.
Danger of IO duplicate
# 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