Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do we pass 0 as a parameter to "exit"?

Tags:

ruby

exit

In the book learn ruby the hard way, I found a syntax to exit from the program:

Process.exit(0)

Why is the parameter 0 being passed in the exit method here even though it works if I pass another integer or do not pass any parameter? What is the significance of 0?

like image 911
Bloomberg Avatar asked Mar 21 '13 09:03

Bloomberg


People also ask

What does exit () do in C?

The exit () function is used to break out of a loop. This function causes an immediate termination of the entire program done by the operation system. void exit (int code); The value of the code is returned to the calling process, which is done by an operation system.

Where is exit () defined?

The exit() function is the standard library function of the C, which is defined in the stdlib. h header file.

What does it mean to pass as a parameter?

Parameter passing involves passing input parameters into a module (a function in C and a function and procedure in Pascal) and receiving output parameters back from the module. For example a quadratic equation module requires three parameters to be passed to it, these would be a, b and c.


2 Answers

It is because when a child process is started (child process being your Ruby script in that case) the parent process (shell, system, etc.) can wait for it to finish.

Once it's finished, it can tell the parent process what is the status of it's execution. Zero usually means that the execution has been successfull and completed without any errors.

If you, on example, run your script from bash shell, and it will call Process.exit(0), you could check if it succeeded using $? variable:

$ ./my_ruby.script        # calls Process.exit(0)
$ echo $?
0                         # ok, script finished with no errors.
like image 139
kamituel Avatar answered Sep 28 '22 01:09

kamituel


This is an '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 isn't caught after, this will have no effects.

Here '0' is for 'Everything works fine !'

like image 32
pierallard Avatar answered Sep 28 '22 00:09

pierallard