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
  • 413,090
  • 67
  • 810
  • 771
Louis Magnotti
  • 411
  • 1
  • 4
  • 18

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

;)