Check piped STDIN in Ruby Shell Scripts without Prompting

I was writing a small shell script in Ruby today that needed to check STDIN for data, but I didn’t want to prompt the user to enter data when nothing was found (I only wanted piped in data). This turned out to involve a little more effort than just checking

Luckily, I found a solution at Footle that worked out great. Heres to anyone who needs only piped in data from STDIN without prompting the user.

# body will be set ONLY if data is piped in, and will not
# prompt the user for STDIN otherwise.
require 'fcntl'
body = if STDIN.fcntl(Fcntl::F_GETFL, 0) == 0

View Gist