0

I have a rake task that I would like to be optionally given stdin, e.g. either of the following could work:

rake my_task
cat foo.txt | rake my_task

The problem is that I don't have a reliable way to check whether STDIN was given to the program. Here's my current attempt, which just hangs indeterminately:

instructions = STDIN.read.split("\n") if STDIN.tty?

I added the STDIN.tty? after reading how-can-you-check-for-stdin-input-in-a-ruby-script. It is returning true when I invoke it with rake my_task (no stdin)

max pleaner
  • 26,189
  • 9
  • 66
  • 118

1 Answers1

0

You just need to invert the condition. What you want is to read stdin if it’s not a tty:

instructions = STDIN.read.split("\n") unless STDIN.tty?
max pleaner
  • 26,189
  • 9
  • 66
  • 118
matt
  • 78,533
  • 8
  • 163
  • 197