+ 1

Why it's true?

I assign zero to variable and used this variable in 'if' statement. Why 'if' gives true? https://code.sololearn.com/cI7gd9xYPrwm/?ref=app

23rd Sep 2017, 3:55 PM
Siroj Samadov
Siroj Samadov - avatar
2 Answers
+ 19
In Ruby, only 'false' and 'nil' are evaluated as false. 0 is evaluated as true and that's why the output of your code is 1.
23rd Sep 2017, 4:06 PM
Krishna Teja Yeluripati
Krishna Teja Yeluripati - avatar
+ 4
x = false puts x ? 1 : 0 try this way only true and false are boolean. 0 is not a boolean
23rd Sep 2017, 4:01 PM
ŠœŠ³. ŠšŠ½Š°ŠæšŸŒ 
ŠœŠ³. ŠšŠ½Š°ŠæšŸŒ  - avatar