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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T21:46:55+00:00 2026-06-09T21:46:55+00:00

Possible Duplicate: What does ||= (or equals) mean in Ruby? Out on the internet

  • 0

Possible Duplicate:
What does ||= (or equals) mean in Ruby?

Out on the internet I’ve seen the following syntax in Ruby/Rails:

user ||= User.new

I’m a newbie and I can’t parse this. Can someone explain to me what the “||=” operator does?

  • 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-09T21:46:57+00:00Added an answer on June 9, 2026 at 9:46 pm

    If user is already set this does nothing, otherwise it will assign a new User object (created with User.new).

    According to David A. Black, author of “The Well-Grounded Rubyist”:

    x ||= y means: x || x = y

    The difference is that x ||= y won’t complain if x is undefined,
    whereas if you type x || x = y and there’s no x in scope, it will.

    For some added details, here’s the relevant part of parse.y:

    | var_lhs tOP_ASGN command_call
    {
      /*%%%*/
      value_expr($3);
      if ($1) {
        ID vid = $1->nd_vid;
        if ($2 == tOROP) {
          $1->nd_value = $3;
          $$ = NEW_OP_ASGN_OR(gettable(vid), $1);
          if (is_asgn_or_id(vid)) {
            $$->nd_aid = vid;
          }
        }
        else if ($2 == tANDOP) {
          $1->nd_value = $3;
          $$ = NEW_OP_ASGN_AND(gettable(vid), $1);
        }
        else {
          $$ = $1;
          $$->nd_value = NEW_CALL(gettable(vid), $2, NEW_LIST($3));
        }
      }
    

    NEW_OP_ASGN_OR is defined in node.h:

    #define NEW_OP_ASGN_OR(i,val) NEW_NODE(NODE_OP_ASGN_OR,i,val,0)
    

    NEW_NODE looks like this:

    #define NEW_NODE(t,a0,a1,a2) rb_node_newnode((t),(VALUE)(a0),(VALUE)(a1),(VALUE)(a2))
    

    Looking for NODE_OP_ASGN_OR leads to compile.c, where the interesting part looks like this:

    case NODE_OP_ASGN_OR:{
       LABEL *lfin = NEW_LABEL(nd_line(node));
       LABEL *lassign;
    
       if (nd_type(node) == NODE_OP_ASGN_OR) {
         LABEL *lfinish[2];
         lfinish[0] = lfin;
         lfinish[1] = 0;
         defined_expr(iseq, ret, node->nd_head, lfinish, Qfalse);
         lassign = lfinish[1];
         if (!lassign) {
           lassign = NEW_LABEL(nd_line(node));
         }
         ADD_INSNL(ret, nd_line(node), branchunless, lassign);
       }
       else {
         lassign = NEW_LABEL(nd_line(node));
       }
    
       COMPILE(ret, "NODE_OP_ASGN_AND/OR#nd_head", node->nd_head);
       ADD_INSN(ret, nd_line(node), dup);
    
       if (nd_type(node) == NODE_OP_ASGN_AND) {
         ADD_INSNL(ret, nd_line(node), branchunless, lfin);
       }
       else {
         ADD_INSNL(ret, nd_line(node), branchif, lfin);
       }
    
       ADD_INSN(ret, nd_line(node), pop);
       ADD_LABEL(ret, lassign);
       COMPILE(ret, "NODE_OP_ASGN_AND/OR#nd_value", node->nd_value);
       ADD_LABEL(ret, lfin);
    
       if (poped) {
         /* we can apply more optimize */
         ADD_INSN(ret, nd_line(node), pop);
       }
       break;
     }
    

    I think this is more than I ever wanted to know about assignment in Ruby, but it was rather entertaining to look this up.

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

Sidebar

Related Questions

Possible Duplicate: What does ||= (or equals) mean in Ruby? It's hard to search
Possible Duplicate: What does map(&:name) mean in Ruby? Ruby/Ruby on Rails ampersand colon shortcut
Possible Duplicate: What does ||= (or equals) mean in Ruby? What does ||= mean?
Possible Duplicate: What does “(void) new” mean in C++? I'm not familiar with C++
Possible Duplicate: What does += (plus equals) mean? What is the point of sum
Possible Duplicate: What does this construct (x = x || y) mean? I've seen
Possible Duplicate: What does the question mark and the colon (?: ternary operator) mean
Possible Duplicate: Best practices regarding equals: to overload or not to overload? Does anyone
Possible Duplicate: Does a finally block always run? let's imagine the following scenario: public
Possible Duplicate: What does “DateTime?” mean in C#? What does the ? mean after

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.