ruby 退出和中止有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23340609/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the difference between exit and abort?
提问by x-yuri
The abortdocumentation says abortwill
该abort文件说, abort将
Terminate execution immediately, effectively by calling Kernel.exit(false).
通过调用 Kernel.exit(false) 立即终止执行。
What exactly does "immediately" mean? What is the difference between abortand exitwith non-truestatus?
“立即”究竟是什么意思?非状态abort和exit非true状态有什么区别?
回答by quetzalcoatl
"Exit, Exit! Abort, Raise…Get Me Outta Here!" describes everything you'd want to know I think.
“退出,退出!中止,提升……让我离开这里!”描述了我认为你想知道的一切。
In short:
简而言之:
Kernel.exit(code)"exits" the script immediately and returns thecodeto the OS, however, just before doing it, it calls any registeredat_exithandler that your code could have registered.Kernel.exit!(code)does the same, but exits immediatelly, noat_exithandlers called.Kernel.abort(message)takes amessagethat will be printed to STDERR just before exiting with a failure code=1.
Kernel.exit(code)立即“退出”脚本并将 返回code给操作系统,但是,在执行此操作之前,它会调用at_exit您的代码可能已注册的任何已注册处理程序。Kernel.exit!(code)做同样的事情,但立即退出,没有at_exit调用处理程序。Kernel.abort(message)取message,将仅有一个故障代码= 1退出之前被打印到stderr。
Different values of exit codes are barely suitable for detecting problems and debugging the code. However, they are very simple to use and making the parent process read them is almost trivial. Hence, exitand exit!.
退出代码的不同值几乎不适合检测问题和调试代码。然而,它们使用起来非常简单,让父进程读取它们几乎是微不足道的。因此,exit和exit!。
If you can spend more time and make the error checking more robust, you'll need some serious error messages, not just codes. Traditionally, you can print them to STDERR if it exists. You can print manually to STDERR via normal puts, but exit-codes will still be used at the lowest level.
如果您可以花更多时间并使错误检查更加健壮,您将需要一些严重的错误消息,而不仅仅是代码。传统上,您可以将它们打印到 STDERR(如果存在)。您可以通过 normal 手动打印到 STDERR puts,但退出代码仍将在最低级别使用。
Printing to STDERR does not mark your job automatically as failed, so, abortwas created to allow you to write and quit easily. A default exit code of 1 is enough to mark the FAIL condition, as it's assumed that all the real contextual information will be included in the error messages provided by you.
打印到 STDERR 不会自动将您的作业标记为失败,因此,abort创建它是为了让您轻松编写和退出。默认退出代码 1 足以标记 FAIL 条件,因为它假定所有真实的上下文信息都将包含在您提供的错误消息中。
Also note that any unhanded exceptions, such as raise "wtf"with no rescueanywhere, actually behave as if calling Kernel.abort: they print to STDERR and use exitcode=1.
还要注意,任何未经处理的异常,例如raise "wtf"没有rescue任何地方,实际上就像调用Kernel.abort: 它们打印到 STDERR 并使用exitcode=1.
You said exit(false)but the exit!documentation says that the parameter is status codeto be used.
你说exit(false)但exit!文档说status code要使用该参数。
I've just checked that on Windows and Ruby 1.9.3:
我刚刚在 Windows 和 Ruby 1.9.3 上检查过:
exit 0 # quits with code: 0
exit 1 # quits with code: 1
exit false # quits with code: 1
exit true # quits with code: 0
which really surprises me, as I'd assume that falsewould be coerced to 0in the traditional C way. So, maybe you should rather be using integers like 0or 1to be perfectly clear about what code will be used.
这真的让我感到惊讶,因为我认为这false会以0传统的 C 方式强制执行。所以,也许你应该更喜欢使用整数,0或者1完全清楚将使用什么代码。

