Exit, Stage left.
Every time you log into an irb session, load a ruby script, or run a test run, you start the process. This applies to everything on your system, not just Ruby code. For example, the same is true for shell commands such as
We spend a lot of time and effort discussing the right way to write code and ensure it runs efficiently, but what about how it will make its mark on the world? There is a precious ability to effectively convey information when exiting the process.
This article sheds light on why the right output is important and how you can make it out of your Ruby programs.
Code, exit view
When a process completes, it always does so with an exit code. An exit code is a numeric value between 0 and 255. In the Unix world, there is a convention that an exit code of 0 means that the program was successful. Any other exit code indicates some kind of failure.
Why do output codes matter?
Exit codes are a link.
The basic option for exit codes is success, you hope your process completes successfully every time. In case something goes wrong, you can specify up to 255 different reasons for the failure. Exit codes are critical to providing a common way of understanding and reporting opt-out cases to users so they can fix the action.
Also, if you're not sure if exit codes are something worth caring about, think about HTTP status codes; in the case of an error response, there are many status codes that indicate why the request failed, which tell the user what to do next. Do you think HTTP status codes are inspired?
Example without Ruby
We'll start by looking at
the grep command Note that I'm using zsh with
$ echo foo | grep bar zsh: done echo foo | zsh: exit 1 grep bar
In this example, we typed the string "foo" and added "bar". Obviously, that won't fit. zsh informs us that
the echo command
is the grep command for
the grep command
Compare that to:
$ echo foo | grep foo foo
In this example, it could match the string 'foo' and completed successfully. Also, zsh doesn't tell us that everything went well.
Now let's see what happens when we send the wrong option to
$ echo foo | grep --whodunnit foo grep: unrecognized option `--whodunnit' Usage: grep [OPTION]... PATTERN [FILE]... Try `grep --help' for more information. zsh: done echo foo | zsh: exit 2 grep --whodunnit foo
This time we passed an unknown
So, we know that for
grep, if the exit code was 2, then the command is not used properly.