dimanche 28 juin 2015

Placing a bang before an instance variable - !@my_var

The logged_in? method in the code doesn't use the @ sign to reference the current_user instance variable, does this mean that the variable is local to the logged_in? method or does it still reference the @current_user variable? I noticed if I write !@current_user instead of !current_user the method doesn't work correctly. I am slightly confused how this is working under the hood because would if I had a local variable in logged_in? named current_user and I did not want it to reference the @current_user instance variable above.

def current_user
    @current_user ||= User.find_by(id: session[:user_id])
end

def logged_in?
    !current_user.nil?
end

Aucun commentaire:

Enregistrer un commentaire