0
0
Rubyprogramming~5 mins

Truthy and falsy values (only nil and false are falsy) in Ruby - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What are the only falsy values in Ruby?
In Ruby, only false and nil are falsy. Everything else is truthy.
Click to reveal answer
beginner
Is the number 0 truthy or falsy in Ruby?
The number 0 is truthy in Ruby, unlike some other languages where it might be falsy.
Click to reveal answer
beginner
What will this Ruby code print?<br>
if nil
  puts 'Yes'
else
  puts 'No'
end
It will print No because nil is falsy in Ruby.
Click to reveal answer
intermediate
Why does Ruby treat only false and nil as falsy?
Ruby's design makes it simple: only false and nil mean 'no' or 'nothing'. Everything else counts as 'yes' or 'something'.
Click to reveal answer
intermediate
How can understanding truthy and falsy values help you write better Ruby code?
Knowing which values are truthy or falsy helps you write clear conditions and avoid bugs, especially in if statements and loops.
Click to reveal answer
Which of these is falsy in Ruby?
Anil
B0
C"" (empty string)
D[] (empty array)
What does this code print?<br>
puts false ? 'Yes' : 'No'
ANo
Bnil
Cfalse
DYes
Is the string "false" truthy or falsy in Ruby?
ACauses error
BFalsy
CDepends on context
DTruthy
Which values will make an if condition run its block in Ruby?
AOnly true
BOnly non-zero numbers
CAnything except false and nil
DOnly non-empty strings
What is the result of !!nil in Ruby?
Atrue
Bfalse
Cnil
DError
Explain which values are considered falsy in Ruby and why this matters when writing conditions.
Think about what Ruby treats as 'no' or 'nothing'.
You got /4 concepts.
    Describe how Ruby's truthy and falsy values differ from other languages you might know.
    Consider languages where 0 or empty strings might be falsy.
    You got /3 concepts.