Truthy and falsey values¶
A truthy value is a value that is considered true for an if
, unless
, while
or until
guard. A falsey value is a value that is considered false in those places.
The only falsey values are nil
, false
and null pointers (pointers whose memory address is zero). Any other value is truthy.
Logical AND¶
An &&
(and operator) evaluates its left hand side. If it's truthy, it evaluates its right hand side and has that value. Otherwise it has the value of the left hand side. Its type is the union of the types of both sides.
You can think an &&
as syntax sugar of an if
:
some_exp1 && some_exp2
# The above is the same as:
tmp = some_exp1
if tmp
some_exp2
else
tmp
end
Logical OR¶
An ||
(or operator) evaluates its left hand side. If it's falsey, it evaluates its right hand side and has that value. Otherwise it has the value of the left hand side. Its type is the union of the types of both sides.
You can think an ||
as syntax sugar of an if
:
some_exp1 || some_exp2
# The above is the same as:
tmp = some_exp1
if tmp
tmp
else
some_exp2
end