0Pricing
Ruby Academy · Lesson

if, unless, ternary

Branching basics.

Branching Basics

Programs need to make decisions. In Ruby, the if keyword runs a block of code only when a condition is truthy.

An if expression evaluates a condition and, if it holds, runs the code between if and end.

age = 20
if age >= 18
  puts "You are an adult"
end

if / else

Add an else branch to handle the case where the condition is false. Exactly one branch runs.

age = 12
if age >= 18
  puts "Adult"
else
  puts "Minor"
end

All lessons in this course

  1. if, unless, ternary
  2. case/when Expressions
  3. Truthiness in Ruby
  4. Guard Clauses
← Back to Ruby Academy