maxedison
maxedison

Reputation: 17553

Need 'return' statement following 'fail' call in a Ruby method?

I got the following code from a Rails tutorial:

def do_something
    # some code here....

    if @user.blank?
        fail NotAuthenticatedError
        return
    end

    # more code here...
end

Is the return statement necessary, or is the fail call sufficient to stop the rest of the code in this method from running? Perhaps it depends on how the NotAuthenticatedError is handled?

Upvotes: 1

Views: 73

Answers (1)

LanceH
LanceH

Reputation: 1756

No, you don't need the return.

def do_something
    puts "start"
    fail NotAuthenticatedError
    puts "this doesn't print"
end

That code will never get to the last line.

Check out:

what-does-the-fail-keyword-do-in-ruby

Upvotes: 4

Related Questions