0

While programming routes in Sinatra, I came across code listed as this:

before do
  session[:lists] ||= []
end

What is this operation doing ||= []?

mu is too short
  • 426,620
  • 70
  • 833
  • 800
Louis Magnotti
  • 431
  • 1
  • 4
  • 19

2 Answers2

2

x ||= value is a way to say "if x contains a falsey value, including nil, assign value to x"

That's setting session[:lists] equal to [] if session[:lists] is falsey.

Related to https://stackoverflow.com/a/6671466/4722305.

Community
  • 1
  • 1
1

It sets [] to session[:lists] when it's nil or falsy

Read more here

;)