Valko Sipuli Valko Sipuli - 1 year ago 172
Ruby Question

Ruby system exit status constants

The Ruby

takes a status code argument. This code corresponds to the
C function call, for which C libraries provide predefined constants. Using constants is more readable than using literal integers. The macOS
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
quoted above? Preferably in the language itself, standard library or if nothing else then as a Ruby Gem.

Answer Source

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;
  case Qfalse:
    istatus = EXIT_FAILURE;
  // ...

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download