O'Reilly logo

Sams Teach Yourself Ruby in 21 Days by Mark Slagell

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

The Art of Instant Reproduction

Something special happens when we supply the special filename “-” to IO.popen. Instead of starting an external program, the Ruby interpreter makes an exact copy of itself as a new process, running the same script as before:

IO.popen("-", "w+")

# Now there are two of me!
["fee", "fie", "foe"].each {|x| STDERR.puts x}

 # output: feefee
						fie
						foe
						fie
						foe
					

The duplicate script does not start at the beginning but from the point immediately after the IO.popen call. Parent and child are running overlapped in time—or if you prefer, at the same time—so there is no guarantee of the order in which their output to STDERR will appear. That's why we see their output interleaved. Here, one has rudely interrupted the other before ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required