Boolean Values
Boolean values are true and false, like a light switch that is on or off. Ruby uses them to answer yes-or-no questions.
In this page:
Syntax
flag = true
other = false
result = value1 > value2
Boolean Values
Ruby has true and false as the literal boolean values, both instances of their own classes (TrueClass and FalseClass). Comparison and logical operators produce these values directly.
Ruby's boolean handling is simple, but its rules for what counts as "truthy" in a condition are broader than just true/false, covered in a later chapter.
Note:
true and false are singleton objects in Ruby, each the sole instance of their own class.
Example: Boolean Values
is_active = true
is_admin = false
puts is_active.class
puts is_active && is_admin
puts 10 > 5
Login to try C/C++/Java/PHP code in the editor
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the
view already swaps in the hi_ translation fields (or blanks these
out if untranslated), so this renders correctly for both languages
without a lang_code check here. #}
Common Mistakes
- Expecting
0or an empty string to be false, when in Ruby onlyfalseandnilare falsy. - Writing
TrueorTRUE, which Ruby treats as a constant that is not defined. - Using
and/orwhere&&/||are meant, since they have lower precedence and can change the result.
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: