Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Ruby脚本中检查STDIN输入?_Ruby - Fatal编程技术网

如何在Ruby脚本中检查STDIN输入?

如何在Ruby脚本中检查STDIN输入?,ruby,Ruby,我需要检查Ruby脚本中是否存在STDIN输入,就像mysql命令一样。如果没有任何内容指向STDIN,则脚本不应尝试读取STDIN 如何以跨平台的方式做到这一点?这是Linux中经常做的事情: #!/usr/bin/env ruby str = (STDIN.tty?) ? 'not reading from stdin' : $stdin.read puts str >> $ ruby test.rb >> not reading from stdin >

我需要检查Ruby脚本中是否存在STDIN输入,就像
mysql
命令一样。如果没有任何内容指向STDIN,则脚本不应尝试读取STDIN


如何以跨平台的方式做到这一点?

这是Linux中经常做的事情:

#!/usr/bin/env ruby

str = (STDIN.tty?) ? 'not reading from stdin' : $stdin.read
puts str

>> $ ruby test.rb 
>> not reading from stdin
>> $ echo "reading from stdin" | ruby test.rb 
>> reading from stdin

使用
STDIN
$STDIN
的具体原因是什么?