我想知道是否有人试图在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。我不想修改调用命令,但希望支持两种行为。