Possible Duplicate:
What does ||= mean in Ruby?
What does ||= mean in Ruby?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
It’s mainly used as a shortform for initializing a variable to a certain value, if it is not yet set.
Think about the statement as returning
x || (x = y). Ifxhas a value (other thanfalse), only the left side of the||will be evalutated (since||short-circuts), andxwill be not be reassigned. However, ifxisfalseornil, the right side will be evaluated, which will setxtoy, andywill be returned (the result of an assignment statement is the right-hand side).See http://dablog.rubypal.com/2008/3/25/a-short-circuit-edge-case for more discussion.