I have a rake task where I do some checks at the beginning, if one of the checks fails I would like to return early from the rake task, I don't want to execute any of the remaining code.
I thought the solution would be to place a return where I wanted to return from the code but I get the following error
unexpected return
Return with an Error
If you're returning with an error (i.e. an exit code of 1) you'll want to use
abort
, which also takes an optional string param that will get outputted on exit:On the command line:
Return with Success
If you're returning without an error (i.e. an exit code of 0) you'll want to use
exit
, which does not take a string param.On the command line:
This is important if you're using this in a cron job or something that needs to do something afterwards based on whether the rake task was successful or not.