我编写了一个Ruby脚本,如下例所示.基本功能是相同的:
# get input from the user
input = gets.chomp
# do awesome stuf with this input and print the response
puts do_awesome_stuff(input)
Run Code Online (Sandbox Code Playgroud)
问题是,当我运行脚本时,它会打印出我想要的解决方案,但控制台窗口会立即关闭.我希望控制台保持开放状态.我目前正在使用Windows,但解决方案应该适用于每个系统.
一种方法是使用.bat文件运行ruby脚本并暂停它,如下所示:
ruby script.rb
PAUSE
Run Code Online (Sandbox Code Playgroud)
我希望有一种方法没有额外的.bat文件.Ruby是否具有集成PASUE的功能?
看起来你双击ruby脚本文件.
而是在cmdshell中发出以下命令.
ruby filename.rb
Run Code Online (Sandbox Code Playgroud)
如果您不想这样,可以添加gets到脚本的末尾.
# get input from the user
input = gets.chomp
# do awesome stuf with this input and print the response
puts do_awesome_stuff(input)
gets # <----
Run Code Online (Sandbox Code Playgroud)
但是不推荐这样做,因为..如果你在cmd shell或终端中运行命令,你应该输入extra Enter来返回shell.