1

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?

maxedison
  • 17,243
  • 14
  • 67
  • 114
  • 1
    fail is similar to raise. so it wont pass that. see this http://apidock.com/ruby/Kernel/fail – Athar Jul 14 '15 at 18:07

1 Answers1

4

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

Community
  • 1
  • 1
LanceH
  • 1,726
  • 13
  • 20