← Back to Ruby Course | Chapter 2: Variables & Types | Lesson 4 of 7

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:

  1. Boolean Values
Syntax
ruby
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

markup
is_active = true
is_admin = false

puts is_active.class
puts is_active && is_admin
puts 10 > 5
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
  1. Expecting 0 or an empty string to be false, when in Ruby only false and nil are falsy.
  2. Writing True or TRUE, which Ruby treats as a constant that is not defined.
  3. Using and / or where && / || 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:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.