Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8678795
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T20:48:47+00:00 2026-06-12T20:48:47+00:00

I’m working through the RoR tutorial by Michael Hartl. Here’s some code from SessionsHelper:

  • 0

I’m working through the RoR tutorial by Michael Hartl. Here’s some code from SessionsHelper:

module SessionsHelper                                                       
  def sign_in(user)                                                         
    cookies.permanent[:remember_token] = user.remember_token                
    self.current_user = user                                                
  end                                                                       

  def current_user=(user)                                                   
    @current_user = user                                                    
  end                                                                       

  def current_user                                                          
    @current_user ||= User.find_by_remember_token(cookies[:remember_token]) 
  end                                                                       

  def current_user?(user)                                                   
    user == current_user                                                    
  end                                                                       

  def signed_in?                                                            
    !current_user.nil?                                                      
  end                                                                       

  def sign_out                                                              
    self.current_user = nil                                                 
    cookies.delete(:remember_token)                                         
  end                                                                       

end   

There are a number of things I don’t understand from this code.

  • In the method current_user?, is current_user a variable or is it an invocation of the method current_user?

I don’t understand how to determine the value of current_user in this context.

  • In the method current_user, @current_user is an instance variable. What is it an instance variable of (i.e. what class?)?

If you look under the sign_in(user) method, you can see that self.current_user is assigned the value of the object user. But (assuming self is of the class UsersController because UsersController < ApplicationController and in the definition of ApplicationController there is a line that include SessionsHelper) there is no definition of an instance variable current_user for the self object.

  • How come in current_user= we use the @current_user but in current_user? we just use current_user?

I’ve done h the prerequisite searching on StackOverflow but haven’t found a good explanation for all these differences. A big part of what I’m trying to learn is when to use @current_user, current_user, :current_user. I’m most familiar with C, C++, PHP, Java, JavaScript. If there are analogues to these languages, it would help me understand better what’s going on.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T20:48:48+00:00Added an answer on June 12, 2026 at 8:48 pm

    In the method current_user?, is current_user a variable or is it an
    invocation of the method current_user?

    It’s the method, since there’s no lexical of that name in scope.

    In the method current_user, @current_user is an instance variable.
    What is it an instance variable of (i.e. what class?)?

    It’s an instance variable of an instance of any class which has mixed-in module SessionsHelper (typically with include SessionsHelper).

    If you look under the sign_in(user) method, you can see that self.current_user is assigned the value of the object
    user. But (assuming self is of the class UsersController because
    UsersController < ApplicationController and in the definition of
    ApplicationController there is a line that include SessionsHelper)
    there is no definition of an instance variable current_user for the
    self object.

    sign_in is defined in this module along with a setter method current_user=. self.current_user = user will send the message :current_user= with argument user to self. Since sign_in came from this module it’s quite likely that that message dispatch will go to the setter also defined in this module, though it’s certainly possible that a setter of the same name may be defined by the class which included this module, or by a module included after this module, in which case those methods will be found first in the dispatch search.

    How come in current_user= we use the @current_user but in
    current_user? we just use current_user?

    It’s considered good practise to limit direct instance variable access to the setter and getter methods (and sometimes the initialize) when they are defined. In this case, by reusing the current_user getter in the current_user? predicate we preserve the cache or fetch behaviour of the @current_user ||= User.find_by... expression in the getter.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have a view passing on information from a database: def serve_article(request, id): served_article
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have just tried to save a simple *.rtf file with some websites and
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have a text area in my form which accepts all possible characters from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.