Ruby, instead of exiting when it receives a SIGPIPE, throws an exception Errno:EPIPE which usually results in a stack trace.

./foo.rb:8:in `write': Broken pipe - <STDOUT> (Errno::EPIPE)

Here is the idiomatic one-line to simply exit when your script gets a SIGPIPE:

trap('PIPE', 'EXIT')