if, elsif, else: the full shape

Free preview 3 min to read

A familiar shape, finally named completely

if as a trailing modifier showed up in Ruby: programmer happiness. Ruby methods & returns used full if/elsif/else blocks in classify and check_age without naming the shape. This course covers it fully: conditionals, &&, || and the standard checks every Ruby program relies on.

The complete block

def classify_temperature(degrees)
  if degrees >= 30
    "hot"
  elsif degrees >= 15
    "warm"
  else
    "cold"
  end
end

classify_temperature(35)   # => "hot"
classify_temperature(20)   # => "warm"
classify_temperature(5)    # => "cold"

Ruby checks each condition top to bottom and runs the body of the first truthy one, using the truthiness rule from Ruby data: numbers & booleans. Once one matches, the remaining elsifs aren't evaluated.

As many elsif branches as you need

You can chain as many elsifs as the cases need, each checked in order until one matches.

def grade_letter(score)
  if score >= 90
    "A"
  elsif score >= 80
    "B"
  elsif score >= 70
    "C"
  elsif score >= 60
    "D"
  else
    "F"
  end
end

Order matters. A score of 95 matches score >= 90 and stops. It's also true that 95 >= 80, but that branch is never reached.

else is optional

else is optional. Without it, if nothing matches, the if block evaluates to nil, quietly, with no error.

# Translates a weekend day name into Portuguese; any other day has no answer.
def weekend_name(day)
  if day == "Saturday"
    "Sábado"
  elsif day == "Sunday"
    "Domingo"
  end
end

weekend_name("Saturday")   # => "Sábado"
weekend_name("Tuesday")    # => nil, no branch matched, and there's no else

That ties into implicit return from Ruby methods & returns. The if block is weekend_name's last expression, so its value, nil here, is what the method returns.

Every if is itself an expression

This explains why those examples work. An if/elsif/else block is an expression with its own value, like 5 + 3 or a method call. You can assign it, return it or use it directly.

status = if degrees >= 30
           "hot"
         else
           "not hot"
         end

That's why classify_temperature and grade_letter need no return. The if block produces a value, and it flows out of the method.

What this course covers

  • Lesson 1, if, elsif, else: the complete conditional block, named at last (you are here)
  • Lesson 2, unless: the full block form, and when to avoid it
  • Lesson 3, Combining conditions with &&, || and !: building compound checks from simple ones
  • Lesson 4, Short-circuit evaluation: why the order of a compound condition matters
  • Lesson 5, and / or: the word versions, why they exist, and the trap in using them
  • Lesson 6, case / when: a cleaner alternative to a long elsif chain
  • Lesson 7, Putting it together: a small challenge combining everything

By the end, you'll know every conditional shape in real Ruby code and when to use each.

Lesson 1 of 7 · free

Keep going with lesson 2

unless: the direct inverse of if

The block form of unless, its use as a guard clause, and why RuboCop discourages pairing it with else.

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

Type to start searching...