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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T03:38:37+00:00 2026-05-23T03:38:37+00:00

Hey I was following Michael Hartl’s railstutorial when I stumbled upon this chunk of

  • 0

Hey I was following Michael Hartl’s railstutorial when I stumbled upon this chunk of code.

Does anyone have an idea what the “user &&” is used for by the ternary operator?

Here’s the code:

def self.authenticate(email, submitted_password)
  user = find_by_email(email)
  user && user.has_password?(submitted_password) ? user : nil
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-23T03:38:37+00:00Added an answer on May 23, 2026 at 3:38 am

    && is a logical and except that it has a higher precedence than and. So the statement just says if user isn’t nil and user has that password then return user, else return nil.

    In Ruby, the second part of a logical and isn’t executed if the first part is false. So the purpose here is to make sure there’s a user before calling has_password? on it, thus preventing the error of calling has_password? on nil. Another way to do this would be to use try, e.g.:

    user.try(:has_password?, submitted_password) ? user : nil
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hey Guys I have the following simple Code : WhereAmIViewController.h #import <UIKit/UIKit.h> #import <CoreLocation/CoreLocation.h>
Hey guys I have the following code working which is sending a GET Request
Hey - I have the following method on my UITableView controller public void RemoveEventFromList(Event
hey, following problem here: i have table with persons that have one location, if
Hey. I have the following markup: <!DOCTYPE html PUBLIC -//W3C//DTD XHTML 1.0 Transitional//EN http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd>
I have the following JSON: { @id: hey there, id: hey there 2 }
Hey there, I'm trying to make a site which have following: News, Products, About
Hey guys ive got the following code in my Model require 'RMagick' class Upload
Hey I have the following situation: a WinService which is processing some stuff from
Hey I'm trying to compile the following piece of code to basically read stuff

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.