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.

Thanks!

Did you find this post useful? Show some love!
DISCUSSION (14)

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)
else
'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
else
  @user = User.first
  return @user
end

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

Hence

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.

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

Hating on languages you don't use

Please dont. Every language is good at something. Your taste, experience and ...

Alex
CTO @ Prevent A Lemon. Platform Developer @ Grassriots Inc. Runner, dog person.

New to dev.to?

Browse All-Time Top Posts