Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why 0 && 1 is 1 while 1 && 0 is 0 in ruby?

Tags:

In Ruby, why are the following lines true?

0 && 1 == 1 1 && 0 == 0 

Why are they different and aren't both 0?

like image 758
CEGRD Avatar asked Mar 11 '12 10:03

CEGRD


1 Answers

Boolean AND operator && returns its second operand if first is not false. 0 and 1 are true in boolean expressions in Ruby. Only nil and false are false in boolean expressions.

nil && 15 # => nil 15 && 17 # => 17 15 && nil # => nil 
like image 148
Aliaksei Kliuchnikau Avatar answered Sep 26 '22 12:09

Aliaksei Kliuchnikau