It's like the Null Coalescing Operator from PHP :

A common misconception is that a ||= b is equivalent to a = a || b, but it behaves like a || a = b

Here's a link explaining in Ruby :


I'm glad to read that I'm not the only one who has been confused by this! Thanks for the explanation and links, Valentin.


Stop liking my comment !
It's false, it's not a Null coalescing.

Ruby :
a || = b

PHP equivalent :
$a = $a != null ? $a : $b;

Or in "pseudocode" :
If 'a' is not null
'a' is equal to 'a' (itself)
'a' is equal to 'b'


Ruby/Rails developer here, as often in Ruby, you have to read it "in english" to understand what it does: or equals.

So @user ||= User.first reads "@user OR equals User.first", which means "Take @user value (if defined) OR (if not) make @user equal to (as in 'taking the value of') User.first (and then take the new @user value)".

You could code it this way too:

if not @user.nil
  return @user
  @user = User.first
  return @user

Ruby does not have an "defined-or" operator! The ||= evaluates the right side even when the left side is defined and false. Example:

irb(main):001:0> foo = false
=> false
irb(main):002:0> foo
=> false
irb(main):003:0> foo ||= true
=> true
irb(main):004:0> foo
=> true

Excellent explanation! I love Ruby for it's readability, and shortcut syntax breaks that, but rewriting it in "long hand" really helps.

When I first learned @user.present? ? puts "Hello, friend" : puts "I'm so lonely" syntax for if\else statements, it melted my mind. But once I wrote it out long hand and then "translated" it, it was so much more clear.

Thanks again!


BTW .present? is Rails only, or least defined only in ActiveSupport

Another "trick" I like is .presence which transforms:

object.present? ? object : nil

to just object.presence

Ah yes, thank you! I do use .presence sometimes too. I often forget what magic Rails has added, and what magic is native :)

Yeah, I've only once once wrote a web app without Rails and I kept getting errors because I kept calling methods that didn't exist outside of it :D

Ha! I know the struggle. That said, I've started using Sinatra for some little middleware apps and I really love it though. It's so quick and easy to build something simple and get it up on Heroku.


Consider the following.

a = 1

a += 1  # 'a' would be 2 here.
# is the same as
a = a + 1


a = 1 

a ||= 2 # 'a' would be 1 here.
# which is just another way to write 
a = a || 2 

I'm not a Ruby developer, but is it similar to a ternary operator? Basically saying if user variable is not set then get the first user from your model.


That's what I assume from context too, but I'm not clear on the specifics.

Classic DEV Post from Jul 13 '18

Fixing Mistakes with git

How to roll back accidental changes with git, grouped by what happened and how far the changes ended up

CTO @ Prevent A Lemon. Platform Developer @ Grassriots Inc. Runner, dog person. is where software developers stay in the loop and avoid career stagnation.

Sign up (it's free!)