We write scripts to automate setup and deployment of Rails applications.
In those scripts, at many places, we need to run system commands like
bundle install, rake db:create, rake db:migrate and many more.
Let’s suppose we need to run migrations using
rake db:migrate in a rails project setup script.
We can use Kernel#system method.
Executing system returns true or false.
Another feature of system is that it eats up the exceptions.
Let’s suppose our migrations can run successfully. In this case system command
for running migrations will return true.
Let’s suppose we have a migration which is trying to add a column to a table which
does not exist. In this case, system command for running migrations will return
As we can see, even when there is a failure in executing system commands, the return
value is false. Ruby does not raise an exception in those cases.
However, we can use raise explicitly to raise an exception and halt the setup
Ruby 2.6 make our lives easier by providing an option exception: true so that
we do not need to use raise explicitly to halt script execution.
Ruby 2.6 works the same way as previous ruby versions when used
without exception option or used with exception set as false.