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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T14:43:54+00:00 2026-05-25T14:43:54+00:00

Confused by this method – can someone please explain it to me? def current_user

  • 0

Confused by this method – can someone please explain it to me?

def current_user
    @current_user ||= (login_from_session || login_from_cookie) unless @current_user == false
end
  • 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-05-25T14:43:55+00:00Added an answer on May 25, 2026 at 2:43 pm

    It says:

    1. unless current is not set (the latest unless)
    2. do nothing and return @current_user if @current_user is already set (the ||= part)
    3. else call the method/helper login_from_session and assign the result to @current_user
    4. else if the previous call has returned nil or false, call the method/helper login_from_cookie and assign the result to @current_user
    5. in any case return the value of @current_user instance variable

    It could be rewritten to be more explicit in this way

    def current_user
      if !(@current_user == false) # 1
        if (@current_user)
          return @current_user # 2
        end
        if (@current_user = login_from_session)
          return @current_user # 3
        end
        if (@current_user = login_from_cookie)
          return @current_user # 4
        end  
      end
      return @current_user # 5
    end
    

    This is the power (and the beauty) of ruby expressiveness. Remember that in Ruby only nil and false evaluate to boolean false in if/else statements and ||, && operators

    Other hints to understand better, in ruby you have the following rules:

    The return value of any function is the last expression evaluated for the function, so

    def foo
      any_value
    end
    

    is the same of

    def foo
      return any_value
    end
    

    The if/unless statement at the end of an expression is the same of a if/unless statement, so

    do something if value
    

    is the same of

    if (value)
      do_something
    end
    

    The ||= operator is a shortcut for

    @a ||= some_value
    # is equivalent to
    if !@a
      @a = some_value
    end
    

    Combine all these rules and you’ve the method explained.

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

Sidebar

Related Questions

I am new to iPhone. Can someone explain this method declaration to me: -
I can't believe I'm still confused about this but, any way, lets finally nail
I am a little confused as to what is being accomplished by this method.
Please explain this simple code: public int fibonacci(int n) { if(n == 0) return
I have confused for this login method, especially for different access page for some
I have this method: def is_active(self): if self.is_current_node(): return True cur_children = self.get_children() while
Totally confused by this one... We have a WAMPServer installation set up, running a
I'm confused about this. Most of us have been told that there isn't any
I'm totally confused on this matter, some say use tables and others say it's
I am a bit confused why this code compiles. I leave out the necessary

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.