Boolean Short Circuit

Posted by Lan Nguyen on August 23, 2020

Boolean Short Circuit

Short circuit evaluation with boolean operators is evaluating the second argument only if the first argument does not satisfy the truthiness or falsiness of the expression. Using the operator “&&” would require that both sides of the operator be truthy, if the left hand side is falsey, it will short circuit and not evaluate the right hand side.

Def test(one, two)
   If one && two
       puts “is true”
       return one && two
   else
	     puts “is false”
	     return one && two
   end
end
Using the “   ” operator, if the left hand side of the operator is truthy, then the right hand side will not be evaluated.
Def test(one, two)
   If one || two
       puts “is true”
       return one || two
   else
	     puts “is false”
	     return one || two
    end
end