Concept Flow - Truthy and falsy values (only nil and false are falsy)
Start
Evaluate value
Is value nil or false?
Yes→Result: false (falsy)
No
Result: true (truthy)
End
Ruby treats only nil and false as false in conditions; everything else is true.
values = [nil, false, 0, "", [], {}] values.each do |v| puts v ? "truthy" : "falsy" end
| Iteration | Value | Condition (v ?) | Result | Output |
|---|---|---|---|---|
| 1 | nil | nil ? => false | false | falsy |
| 2 | false | false ? => false | false | falsy |
| 3 | 0 | 0 ? => true | true | truthy |
| 4 | "" | "" ? => true | true | truthy |
| 5 | [] | [] ? => true | true | truthy |
| 6 | {} | {} ? => true | true | truthy |
| Variable | Start | After 1 | After 2 | After 3 | After 4 | After 5 | After 6 | Final |
|---|---|---|---|---|---|---|---|---|
| v | nil | false | 0 | "" | [] | {} | {} | {} |
In Ruby, only nil and false are falsy. Everything else (0, empty string, empty array, etc.) is truthy. Use conditions like if value to check truthiness. This differs from some other languages. Remember: nil and false mean false; all else means true.