Valko Sipuli Valko Sipuli - 3 months ago 14
Ruby Question

Ruby system exit status constants

The Ruby

takes a status code argument. This code corresponds to the
exit(3)
C function call, for which C libraries provide predefined constants. Using constants is more readable than using literal integers. The macOS
exit(3)
man page says:

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.


Does Ruby provide constants that correspond to the status codes such as
EXIT_SUCCESS
quoted above? Preferably in the language itself, standard library or if nothing else then as a Ruby Gem.

Answer

As stated in the documentation for Kernel#exit:

true and FALSE of status means success and failure respectively.

More specifically:

  • exit(true) (or just exit) corresponds to EXIT_SUCCESS
  • exit(false) corresponds to EXIT_FAILURE

The underlying C code:

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

Other values are system dependent and therefore don't have a constant in the C standard.

Comments