Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我正在编写一个小型交互式命令行工具,提示用户按数字键。它应该在第一次按键/输入后直接继续。
目前我这样做是为了捕获用户的输入
puts "yes, please ..." gets.chomp
...但是这需要按“输入”来确认输入。第一次按键后如何立即返回输入值?
尝试这样的事情:
puts 'Do you want to proceed? y/n' loop do system("stty raw -echo") c = STDIN.getc system("stty -raw echo") case c when 'y' puts 'Yes' break when 'n' puts 'No' break else puts 'Please type "y" or "n"' end end