检测Ruby中的stdin内容

检测Ruby中的stdin内容,第1张

概述我想知道是否有人试图在stdin上提供 ruby程序内容.我不希望 ruby回退到允许交互式输入.我该怎么做呢? # When called in bash like this, I want 'cat.rb' to exit immediately:ruby cat.rb# When called in bash like this, I want to see the word 'hel 我想知道是否有人试图在stdin上提供 ruby程序内容.我不希望 ruby回退到允许交互式输入.我该怎么做呢?

# When called in bash like this,I want 'cat.rb' to exit immediately:ruby cat.rb# When called in bash like this,I want to see the word 'hello':echo hello | ruby cat.rb

如果我只有cat.rb包含puts gets,那么第一个示例将阻塞,等待交互式stdin上的EOF.我不想修改调用命令,但希望支持这两种行为.

解决方法 看看$stdin.tty?

ruby cat.rb# $stdin.tty? -> trueecho hello | ruby cat.rb# $stdin.tty? -> false
总结

以上是内存溢出为你收集整理的检测Ruby中的stdin内容全部内容,希望文章能够帮你解决检测Ruby中的stdin内容所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/langs/1293408.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-10
下一篇 2022-06-10

发表评论

登录后才能评论

评论列表(0条)

保存