Recently I saw code like
i < 0 ||
I wonder what is "||" exactly mean? How to say that in english?
Recently I saw code like
i < 0 ||
I wonder what is "||" exactly mean? How to say that in english?
Logical "or"
A || B
is true
when either A
is true or B
is true, or when both A
and B
are true.
The ||
operator is similar to the keyword or
but is different from the keyword or
in extremely important ways. Below are two great write-ups on the topic, comparing the two and showing you how to use either one:
The most important thing to note in what Avdi says, is that ||
cannot be used for flow control, whereas or
can be.
For example...
a = :value
c = b || a
#de Since `b` is undefined/null, `c` will be set to `:value`
c = b || puts("Failure!") #de This will raise an exception!
c = b or puts("Failure!") #de Will set `c` to `NilClass` and output "Failure!"