What does ||= do?

Hey DEV! Can you explain to me what ||= does in Ruby? For example, @user ||= User.first. I can make some assumptions from the contexts in which I've seen it used, but I haven't heard a proper explanation of how it works, and it's a difficult syntax to DuckDuckGo for.


Did you find this post useful? Show some love!

It's like the Null Coalescing Operator from PHP : php.net/manual/en/language.operato...

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 : rubyinside.com/what-rubys-double-p...

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'

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.

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

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.

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

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 

Hey there, we see you aren't signed in. (Yes you, the reader. This is a fake comment.)

Please consider creating an account on dev.to. It literally takes a few seconds and we'd appreciate the support so much. ❤️

Plus, no fake comments when you're signed in. 🙃

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.

it is similar, where it's like:
a = (a ? a : 2)

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

Classic DEV Post from Jun 26

Books vs Online Courses

Which the two learning methods is better for learning a new programming language?

Follow @kostassar to see more of their posts in your feed.
CTO @ Prevent A Lemon. Platform Developer @ Grassriots Inc. Runner, dog person.
More from @acflint
Location Specific Landing Pages
#rails #ruby #help
Trending on dev.to
How to Reduce Memory Usage for Rails and Sidekiq
#ruby #rails #sidekiq #webdev
Please keep using Ruby
#ruby #healthydebate
Explain JavaScript Promises like I am five.
#discuss #explainlikeimfive #javascript
What is the difference between Methods, Computed, and Watchers?
#explainlikeimfive #vue #javascript
Pieces of an App: The Files You Need to Start a Rails App
#ruby #beginners #webdev
Why avoid Ruby's for .. in?
#healthydebate #discuss #ruby
Building My First Sinatra-based Ruby App: Thoughts and Wrap-up
#sinatra #ruby #beginners
Methods, blocks and sorting in Ruby