Ruby系统退出状态常量

时间:2016-09-02 07:30:25

标签: ruby

Ruby Kernel#exit采用状态代码参数。此代码对应于exit(3) C函数调用,C库为其提供预定义常量。使用常量比使用文字整数更具可读性。 macOS exit(3)手册页说:

The C Standard (ISO/IEC 9899:1999 (``ISO C99'')) defines the values 0,
EXIT_SUCCESS, and EXIT_FAILURE as possible values of status.  Cooperating
processes may use other values; in a program which might be called by a
mail transfer agent, the values described in sysexits(3) may be used to
provide more information to the parent process.

Ruby是否提供与上面引用的EXIT_SUCCESS等状态代码相对应的常量?最好是在语言本身,标准库或者其他任何东西,然后作为Ruby宝石。

1 个答案:

答案 0 :(得分:2)

正如Kernel#exit的文档中所述:

  状态

trueFALSE分别表示成功和失败。

更具体地说:

  • exit(true)(或只是exit)对应EXIT_SUCCESS
  • exit(false)对应EXIT_FAILURE

基础C code

switch (status) {
  case Qtrue:
    istatus = EXIT_SUCCESS;
    break;
  case Qfalse:
    istatus = EXIT_FAILURE;
    break;
  // ...
}

其他值取决于系统,因此C标准中没有常量。

相关问题