我想知道是否有人试图在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
Run Code Online (Sandbox Code Playgroud)
如果我只是cat.rb包含,puts gets则第一个示例将阻塞,等待交互式stdin上的EOF.我不想修改调用命令,但希望支持这两种行为.
看着 $stdin.tty?
ruby cat.rb
# $stdin.tty? -> true
echo hello | ruby cat.rb
# $stdin.tty? -> false
Run Code Online (Sandbox Code Playgroud)