gpt4 book ai didi

ruby - 如何检查 Ruby 脚本中的 STDIN 输入?

转载 作者:数据小太阳 更新时间:2023-10-29 06:30:58 25 4
gpt4 key购买 nike

我需要检查 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

关于ruby - 如何检查 Ruby 脚本中的 STDIN 输入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4622046/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com