Why do we pass 0 as a parameter for the "exit"?

In the book, learn Ruby the hard way, I found the syntax for exiting the program:

Process.exit(0) 

Why is parameter 0 passed in the exit method here, even if it works, if I pass another integer or don't pass any parameter? What is the value of 0 ?

+8
ruby exit
source share
2 answers

This is the exit code.

This exit code has special meaning in some cases (see, for example, http://tldp.org/LDP/abs/html/exitcodes.html )

You can pass whatever you want, if the code is not caught after, it will have no effects.

Here '0' for 'Everything works fine!'

+12
source share

This is because when the child process starts (the child process, which is your Ruby script in this case), the parent process (shell, system, etc.) may wait for completion.

As soon as he finishes, he can tell the parent process what the status of its execution is. Zero usually means that the execution completed successfully and completed without any errors.

If, for example, you run the shell script from bash and it calls Process.exit(0) , you can check if this succeeded using the $? variable $? :

 $ ./my_ruby.script # calls Process.exit(0) $ echo $? 0 # ok, script finished with no errors. 
+6
source share

All Articles