Boolean And
The &&
(double ampersand) operator is the Boolean And operator. This operator is used to short-circuit at the first occurrence of a falsey value otherwise evaluate to the final truthy value.
> 4 && 5
=> 5
> 4 && nil
=> nil
> 4 && nil && false
=> nil
> 4 && false && nil
=> false
This is typically used as part of an expression where multiple conditions need to be met for the statement to execute.
if account.age < 7.months && domain.unknown?
return :invalid
end
This is not to be confused with the and
operator which has a much lower precedence ↗ than &&
. You'll generally want to use &&
for logical expressions unless you have a specific reason for using and
.
See also: &&=