Ruby Academy · Lesson

case/in Basics

Structural matching.

Lesson 1 of 413 steps

case/in Basics is a free Ruby Academy lesson on CoddyKit. This is lesson 1 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Ruby Academy learning path, and your progress syncs across the web and the CoddyKit app. The Ruby Academy course includes 4 lessons in total.

Pattern Matching Arrives

Ruby's case/in expression performs structural pattern matching: it tests the shape and content of data, not just equality. It is a powerful upgrade over case/when.

case 42
in Integer
  puts 'it is an integer'
end

case/in vs case/when

case/when compares with === (equality-style). case/in deconstructs values and can bind variables. Use in for matching structure.

case 'hello'
in String => s
  puts "string of length #{s.length}"
end

Matching Literals

An in branch can match a literal value, working like a precise equality check.

case 3
in 1 then puts 'one'
in 3 then puts 'three'
in _ then puts 'other'
end

Type Patterns

Naming a class matches any value of that type, leveraging === under the hood.

def describe(x)
  case x
  in Integer then 'number'
  in String then 'text'
  in Array then 'list'
  end
end
puts(describe([1, 2]))

Binding Variables

A bare identifier in a pattern binds the matched value to a new variable you can use in the branch.

case [1, 2]
in [a, b]
  puts(a + b)
end

The Wildcard

The underscore _ matches anything without binding. Use it as a catch-all or to ignore positions.

case 99
in 0 then puts 'zero'
in _ then puts 'something else'
end

Pin Operator

To match against the value of an existing variable (instead of binding a new one), pin it with ^.

expected = 5
case 5
in ^expected
  puts 'matched the pinned value'
end

Alternative Patterns

Combine patterns with | to match any of several alternatives in one branch.

case 'b'
in 'a' | 'b' | 'c'
  puts 'in the first three letters'
end

Binding the Whole Match

Use pattern => name to test a pattern and bind the matched value to a name simultaneously.

case 'Ruby'
in String => word
  puts(word.upcase)
end

NoMatchingPatternError

If no in branch matches and there is no else, Ruby raises NoMatchingPatternError. Add an else to handle the rest safely.

case 10
in 1 then puts 'one'
else
  puts 'no specific match'
end

One-Line Matching

The => operator does a one-line rightward assignment match, raising if it fails. The in operator returns true/false instead.

result = [1, 2] in [Integer, Integer]
puts result

Quick Check

What happens if a case/in has no matching branch and no else?

Recap: case/in Basics

You learned structural matching fundamentals:

  • case/in matches shape, not just equality
  • Literals, type patterns, and the _ wildcard
  • Variable binding and the pin operator ^
  • Alternatives with | and pattern => name
  • Missing match raises NoMatchingPatternError
case 7
in Integer => n then puts(n * 2)
end
Free to start

Learn Ruby with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
29
Lessons
109

Frequently Asked Questions

Is the “case/in Basics” lesson free?

Yes — any 3 lessons of the Ruby Academy learning path, including “case/in Basics”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Ruby Academy course includes 4 lessons in total.

What will I learn in “case/in Basics”?

Structural matching. You practise Ruby Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Ruby Academy?

No prior experience is required. Ruby Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 1 of 4.

How long does the “case/in Basics” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Ruby Academy lesson?

Yes. Every Ruby Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. case/in Basics
  2. Array and Hash Patterns
  3. Find Patterns and Guards
  4. Practical Use Cases
← Back to Ruby Academy