Checking for STDIN in ruby
A colleague was asking today how he could see if there’s data waiting on STDIN
in ruby (on Linux). On OS X, this is pretty straightforward:
That doesn’t work in Linux, though. After some digging, I found this post, which lead to:
Which works on both platforms, but is (a) ugly (catching an exception), and (b) requires you to actually try to read from STDIN
.
In playing around with that, though, I noticed that STDIN.fcntl(Fcntl::F_GETFL, 0)
returned 0
if there was something on STDIN
, and something non-zero (2
in OSX and 32770
in Linux) if STDIN
was empty. So now the code is simple again:
I’m not sure how reliable that is on other platforms (it won’t work on Windows—fcntl is a Unix-y thing). If I’m understanding fcntl.h correctly, a return value of 2/32770 (0x8002) seems to indicate that the io stream in question is open for writing. I’m not sure if that is intended or just a side-effect. Anyone know?