Logic: boolean algebra
This course covers Boolean logic as it appears in Ruby and Rails code. It starts with propositions, the two values true and false, and the origins in the work of George Boole and Claude Shannon. It then covers AND, OR, NOT and XOR, short-circuit evaluation, and Ruby truthiness. Later lessons build truth tables, apply operator precedence and De Morgan's laws, and simplify expressions. The last lesson applies these ideas to boolean columns, scopes, validations, callbacks, views, policy objects and tests.
What you'll learn
- ✓How AND, OR, NOT and XOR work and their truth tables
- ✓Short-circuit evaluation and the difference between && and and, || and or
- ✓Ruby truthiness, the !! pattern and when to use unless
- ✓Building truth tables to prove equivalence and find logic bugs
- ✓Operator precedence, De Morgan's laws and simplification laws
- ✓Boolean logic in Rails scopes, validations, callbacks, views and policy objects
What is boolean logic?
Every time your code makes a decision, it uses Boolean logic: a system where everything is reduced to two values: true or false.
Read the full lesson free →Curriculum
7 lessonsWhat is boolean logic?
Introduces propositions, the values true and false, the three basic operations, and the history behind Boolean logic.
The AND operator
Explains the AND truth table, && versus and, short-circuit evaluation, and AND patterns in Rails code.
The OR operator
Covers the OR truth table, the || default pattern and its trap with false, fetch, and XOR.
The NOT operator
Shows how NOT inverts values, which Ruby values are falsy, the !! pattern, and unless style rules.
Truth tables: mapping every possibility
Teaches building truth tables with 2^n rows, proving equivalence, and using them to debug conditions.
Combining operators: complex expressions
Combines operators using precedence, parentheses, De Morgan's laws, and identity, domination and absorption laws.
Boolean logic in Ruby and Rails: the full picture
Applies Boolean logic to Rails boolean columns, predicate methods, scopes, validations, callbacks, views, policies and tests.