Why it's true? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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