I don’t get why
(var ||= []) << 1
works as expected but
(var ||= true) = false
doesn’t.
Could anyone explain why it doesnt work and what is going on here?
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.
a ||= bbehaves likea || a = b.An assignment returns the assigned value, i.e.,
var = truereturnstrue.var ||= truewill evaluate to the assignmentvar = true, becausevaris undefined at that point. Ifvaris defined and its value istrue, it will return the value ofvar, that istrue; if it’s false, it will return the value oftrue, which istrue.var ||= []returns[], and your first expression evaluated to[] << 1, which is legal.However, your second expression evaluates to
true = false, which throws a compile error.tl;dr
(var ||= []) << 1⟺(var = []) << 1⟺[] << 1✔(var ||= true) = false⟺(var = true) = false⟺true = false✘