What is boolean logic?

Free preview 5 min to read

The language of decisions

Every time your code makes a decision, it uses Boolean logic: a system where everything is reduced to two values: true or false.

Boolean logic is named after George Boole (1815–1864), an English mathematician who formalized the rules of logical reasoning into algebra. His work became the foundation of every digital computer ever built.

Every if statement, every while loop, every database query filter: they all run on Boolean logic.


Only two values

In Boolean logic, there are no maybes. No "sort of." No 0.5. There are two values:

Value Meaning In Ruby
true Yes, on, 1 true
false No, off, 0 false

One Ruby detail matters early: in a condition, only false and nil count as false. Everything else is truthy, including 0 and "". This binary nature is what makes computers possible. At the hardware level, a transistor is either on (electricity flows) or off (it doesn't). Boolean logic maps perfectly to this physical reality.


Propositions

A proposition is any statement that is either true or false: never both, never neither.

# These ARE propositions (they have a truth value)
"Ruby is a programming language"        → true
"5 is greater than 10"                  → false
"The user is logged in"                 → true or false

# These are NOT propositions (no truth value)
"What time is it?"                      → question, not a statement
"Close the door"                        → command, not a statement
"This statement is false"               → paradox, no definite truth value

In programming, propositions become expressions that evaluate to true or false:

user.admin?            # proposition: is the user an admin?
age >= 18              # proposition: is age at least 18?
cart.empty?            # proposition: is the cart empty?

The three fundamental operations

Boolean logic has three basic operations. Every logical expression ever written (no matter how complex) is built from combinations of these three:

Operation Symbol Ruby Meaning
AND ∧ && Both must be true
OR ∨ || At least one must be true
NOT ¬ ! Flips true to false and vice versa

Think of them as the "atoms" of logic. Just as all matter is built from atoms, all logical decisions are built from AND, OR, and NOT.

# AND: both conditions must be true
user.admin? && user.active?

# OR: at least one condition must be true
user.admin? || user.moderator?

# NOT: inverts the condition
!user.banned?

Boolean logic in everyday life

You use Boolean logic every day without realizing it:

Everyday Decision Boolean Expression
"I'll go out if it's sunny AND warm" sunny && warm
"I'll take the bus OR the train" bus || train
"I'll go if it's NOT raining" !raining
"I need a passport AND (a visa OR citizenship)" passport && (visa || citizenship)

Notice the last example uses parentheses to group operations: just like in math. We'll explore operator precedence in a later lesson.


Why Boolean logic matters for Rails developers

Boolean logic isn't just theory: it's the backbone of every Rails application:

Authentication & authorization

# Can this user edit this post?
user.admin? || (user == post.author && !post.locked?)

Database queries

# Find active premium users
User.where(active: true, premium: true)
# SQL: WHERE active = TRUE AND premium = TRUE

Validations

# Validate presence only if published
validates :body, presence: true, if: -> { published? && !draft? }

Control flow

if current_user && current_user.can?(:manage, @course)
  render :edit
else
  redirect_to root_path
end

Mastering Boolean logic means writing cleaner conditions, better queries, and fewer bugs.


George Boole's legacy

Boole published The Laws of Thought in 1854: over 80 years before the first electronic computer. He showed that logical reasoning could be expressed as mathematical equations.

Decades later, Claude Shannon (1937) realized that Boole's algebra could be implemented with electrical switches. This insight (that logic equals circuits) is the foundation of every digital device you use today.

flowchart TB
  A["Boole's algebra (1854)"] --> B["Shannon's circuits (1937)"] --> C["Transistors (1947)"] --> D["Every computer, phone and server running your Rails app"]

Summary

Concept Definition
Boolean logic A system with two values: true and false
Proposition A statement that is either true or false
AND (∧) Both operands must be true
OR (∨) At least one operand must be true
NOT (¬) Inverts the truth value
George Boole Mathematician who formalized logic as algebra (1854)
Claude Shannon Proved Boolean algebra maps to electrical circuits (1937)
Lesson 1 of 7 · free

Keep going with lesson 2

The AND operator

Explains the AND truth table, && versus and, short-circuit evaluation, and AND patterns in Rails code.

A free account also gives you the quizzes, your progress and notes, and the discussion.

Type to start searching...