Found it the hard way.
Improper/misleading:
true or true ? "yes" : "no"
=> true
the above is basically the same as this:
true or (true ? "yes" : "no")
=> true
The proper way:
true || true ? "yes" : "no"
=> "yes"
Found it the hard way.
Improper/misleading:
true or true ? "yes" : "no"
=> true
the above is basically the same as this:
true or (true ? "yes" : "no")
=> true
The proper way:
true || true ? "yes" : "no"
=> "yes"
Comments are closed.
I’m not sure you can call the first ‘improper’; the whole point of having separate &&/|| and and/or in Ruby is that the latter bind very weakly, so can be used for control flow, whereas the former bind rather strongly and can be used for Boolean logic. There are people who say ‘don’t ever use the and/or form’, but I do use it regularly – it’s really useful when you understand the difference. :)
Yes, you are correct that the knowledge of importance order is crucial (I thought I knew it!), yet the first example reads very ambiguous. I’ve amended it to say “Improper/misleading” now.