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 8858075
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T14:44:40+00:00 2026-06-14T14:44:40+00:00

I’m currently reviewing sessions and I came across a pretty generic sessions controller I

  • 0

I’m currently reviewing sessions and I came across a pretty generic sessions controller I used before:

module SessionsHelper

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

def sign_out
    self.current_user = nil
    cookies[:remember_token] = nil
end

def signed_in?
    !current_user.nil?
end

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

def current_user=(user)
    @current_user = user
end


end

And I realized that I used current_user in the views many times. ex:

<% if current_user == @user %>
    <%= current_user.name %>
<% else %>
    This user is not you.
<% end %>
  1. However, I don’t understand why current_user is accessible as if it is an instance variable. Isn’t it a method? Why is it that I can treat it the same as @current_user and pull out attributes from it like current_user.name, current_user.age, etc.? I thought that was only possible for instance variables.

  2. In the first and second methods sign_in(user) and sign_out, what does self refer to? These are considered class methods right?

  • 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-14T14:44:41+00:00Added an answer on June 14, 2026 at 2:44 pm
    1. current_user is indeed a method defined in SessionHelper that just happens to return a reference to its internal instance variable @current_user. Such a method is usually referred to as “reader”. Because the returned value is a reference to @current_user, you can use that value as if it were the actual instance variable, hence current_user.name

    2. The use of self is necessary in the definition of those two methods because without it, ruby will assume that you want to assign to a new local variable called current_user inside of the method instead of calling the method current_user=() already defined inside the module. In this case self refers to the module itself.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I am currently running into a problem where an element is coming back from
I have a view passing on information from a database: def serve_article(request, id): served_article
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all

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.