当我尝试启动脚本时,它会打开,然后立即关闭 [英] When I try to launch a script, it opens and then immediately closes

查看:80
本文介绍了当我尝试启动脚本时,它会打开,然后立即关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个简单的ruby脚本.但是,当我运行它时,命令行会打开,并几乎立即关闭.我在Visual Basic控制台应用程序上遇到了同样的问题,所以我不确定这是否与命令提示符有关.

I am trying to make a simple ruby script. However, when I run it, the command line opens, and closes almost immediately. I had the same problem with a visual basic console application, so I'm not sure if this is a problem with command prompt.

我正在使用Ruby 1.9.3运行Windows 8.感谢您的帮助.

I am running Windows 8 with Ruby 1.9.3. Any help is appreciated.

推荐答案

这是在Windows上开发命令行应用程序时的常见症状,尤其是在使用IDE时.

This is a common symptom when developing command line applications on Windows, especially when using IDEs.

解决问题的正确方法是手动打开命令行提示符或PowerShell,导航到程序所在的目录,然后通过命令行手动执行该程序:

The correct way to solve the problem is to open the command line prompt or PowerShell manually, navigate to the directory where the program is located and execute it manually via the command line:

ruby your_program.rb

这是从一开始就设计命令行程序的方式.当您从IDE运行代码时,它会打开一个终端并告诉它执行您的程序.但是,一旦您的程序完成执行,则终端不再需要执行任何操作,因此将关闭.

This is how command line programs were designed to be executed from the start. When you run your code from an IDE, it opens a terminal and tells it to execute your program. However, once your program has finished executing, the terminal has nothing to do anymore and thus closes.

但是,如果 you 打开终端,那么 you 告诉它要做什么的人,而不是IDE,因此,即使在之后,终端也希望您提供更多输入该程序已完成.它没有关闭,因为没有告诉它关闭.

However, if you open the terminal, then you the one telling it what to do, not the IDE, and thus the terminal expects more input from you even after the program has finished. It doesn't close because you haven't told it to close.

您还可以在Ruby脚本的末尾使用此替代方法:

You can also use this workaround at the end of your Ruby script:

gets

这将从标准输入中读取一行并将其丢弃.它会阻止您的程序(从而使终端)无法完成,直到您按回车键为止.

This will read a line from standard input and discard it. It prevents your program, and thus the terminal, from finishing until you've pressed return.

类似的解决方法可以在任何语言(例如C和C ++)中使用,但我认为它们不能解决实际问题.

Similar workarounds can be used in any language such as C and C++, but I don't think they are solving the actual problem.

但是,请不要因此而气our!在学习时,请随时使用 gets .这是一个非常方便的解决方法,您应该使用它.

However, don't let this discourage you! Feel free to use gets while you are learning. It's a really convenient workaround and you should use it.

请注意,这类hacks不应出现在生产代码中.

Just be aware that these kinds of hacks aren't supposed to show up in production code.

这篇关于当我尝试启动脚本时,它会打开,然后立即关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆