Learn to Code
All tracks
Learn to Code

Free lessons from scratch to pro — every language, databases and DevOps.

AboutContactPrivacy PolicyTerms of Use

Your progress is saved in this browser. © 2026 Learn to Code

RB
Ruby
0/12 completed
Beginner
Hello RubyVariables and typesConditions and loopsMethods
Intermediate
Arrays and hashesBlocks, procs and iteratorsClasses and objectsModules and mixins
Advanced
Exceptions and filesMetaprogramming
Pro
Gems, Bundler and testingRuby on Rails basics
Tracks / Ruby / Beginner

Conditions and loops

Ruby has the usual if and loops, plus a few extras that make code read like sentences: unless, modifiers at the end of a line, and a very flexible case. In practice, you'll loop over collections with methods like each far more often than with while.

if, elsif, else

Ruby · editable

No parentheses around conditions, no braces: blocks close with end. Note the spelling: elsif, not elseif or else if.

Modifiers and unless

A one-line condition can go after the statement. unless means "if not":

Ruby · editable

Use unless only for simple conditions. unless a && !b is hard to read, so use if then.

Comparison and logic

Ruby · editable

<=> (the "spaceship operator") returns -1, 0 or 1, and is what sorting uses. == compares values (1 == 1.0 is true); eql? also checks the type.

case / when

case compares one value against many patterns. Each when can be a value, a list, a range, a class or a regular expression:

Ruby · editable

There's no fall-through (no break needed), and case returns a value too.

Pattern matching

case / in (pattern matching) checks the shape of data and pulls values out of it in one step. It's great for parsed JSON and API responses:

Ruby · editable

name: on its own captures the value into a variable called name. [*, "admin", *] means "an array containing "admin" anywhere".

Loops

Iterating with methods (the Ruby way)

Ruby · editable

%w[apple banana cherry] is a shortcut for ["apple", "banana", "cherry"]. The { |i| … } and do |i| … end parts are blocks, which you'll study properly in Blocks, procs and iterators.

while, until and loop

Ruby · editable
Keyword Does
next Skip to the next iteration (like continue elsewhere)
break Leave the loop (can return a value: break result)
redo Repeat this iteration (rare)

There's also a for x in list loop, but Ruby programmers almost always use each instead.

Finding things without a loop

Many loops you'd write in other languages are single methods in Ruby:

Ruby · editable

FizzBuzz, the Ruby way

Ruby · editable

A case with no value works like a chain of if/elsif.

Practice

  1. Ask for an age with gets and print child, teen or adult using case with ranges.
  2. Print a multiplication table for 7 with 1.upto(10).
  3. Use loop and break to keep asking for a number until the user types 0 (put several lines in the Input box).
  4. Use pattern matching to print the city from { user: { address: { city: "Pune" } } }.
✎ Check your understanding

1.How do you write "else if" in Ruby?

2.What does puts "Sold out" if stock.zero? do?

3.Which keyword skips to the next iteration of a loop?

4.In case x when 1..9, what does the range do?

← Variables and types