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
  • 1 View
  • 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 there, I have the following code: sVal = analogRead(potPin); // read the value
Hey I have the following code which I'm using to isolate the a b
hey well I have the following code loading an swf <object style=visibility: visible; id=myContent
Hey all. I have the following code: http://jsfiddle.net/g7Cgg/ As you can see, there are
Hey, I have the following code, that gives you the effect of a mask.
Hey fellas, I have the following VBA code. It gets the values from an
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 everyone. The following is the code that I am using to play random

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.