What does || = mean?

Possible duplicate:
What does || = (or equal) in Ruby?
What does || = mean?

I just started to learn RubyMotion, and in many examples I can see the syntax ||=. What does it mean?

Here is an example:

def window
  @window ||= begin
    w = UIWindow.alloc.initWithFrame UIScreen.mainScreen.bounds
    w.rootViewController = @navigationController
    w
end

It's hard to search for characters, google ignores the characters in my request.

+5
source share
2 answers

This is an assignment operator that means: or assigns this value to a variable.

So, if you did something like x ||= y, that means x || x = y, therefore, if x is nil or false, set x as the value of y.

+13
source

This statement sets only the variable if the variable is false or Nil.

+1
source

All Articles