0Pricing
Ruby Academy · Lesson

Array and Hash Patterns

Destructure data.

Array and Hash Patterns is a free Ruby Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Ruby Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Array Patterns

An array pattern matches by position and length. Each element pattern is checked against the corresponding element.

case [1, 2, 3]
in [1, 2, 3]
  puts 'exact match'
end

Binding Array Elements

Use identifiers to capture each position into a variable.

case [10, 20]
in [x, y]
  puts(x + y)
end

Length Matters

By default an array pattern requires the exact length. A 2-element pattern will not match a 3-element array.

case [1, 2, 3]
in [a, b]
  puts 'two'
in [a, b, c]
  puts 'three'
end

The Splat for the Rest

A splat *rest captures any number of remaining elements into an array, making length flexible.

case [1, 2, 3, 4]
in [first, *rest]
  puts(first)
  p rest
end

Splat in the Middle

The splat can sit between fixed elements to grab the head and tail while ignoring the middle.

case [1, 2, 3, 4, 5]
in [first, *, last]
  puts("#{first} ... #{last}")
end

Hash Patterns

A hash pattern matches by keys. It checks that the given keys exist and their values match; extra keys are allowed.

case { name: 'Ada', age: 36 }
in { name: 'Ada' }
  puts 'found Ada'
end

Binding Hash Values

Capture a value by giving the key a binding name. Writing just the key binds to a variable of the same name.

case { name: 'Bob', age: 25 }
in { name: String => n, age: Integer => a }
  puts("#{n} is #{a}")
end

Shorthand Key Binding

Omitting the pattern after a key binds a variable named after the key automatically.

case { city: 'Istanbul' }
in { city: }
  puts(city)
end

Requiring No Extra Keys

Hash patterns ignore extra keys by default. Add **nil to require that only the listed keys are present.

case { a: 1 }
in { a: 1, **nil }
  puts 'exactly one key'
end

Nested Patterns

Patterns nest freely, letting you destructure deep structures in a single branch.

data = { user: { name: 'Ada', tags: ['admin', 'dev'] } }
case data
in { user: { name:, tags: [first_tag, *] } }
  puts("#{name}: #{first_tag}")
end

Matching Parsed JSON

Pattern matching pairs beautifully with parsed JSON or CSV data, pulling out exactly the fields you need.

require 'json'
resp = JSON.parse('{"status":"ok","items":[1,2,3]}', symbolize_names: true)
case resp
in { status: 'ok', items: }
  puts(items.sum)
end

Quick Check

By default, will the hash pattern in { a: 1 } match the hash { a: 1, b: 2 }?

Recap: Array and Hash Patterns

You can destructure collections:

  • Array patterns match by position and length
  • *rest captures remaining elements
  • Hash patterns match by key, allowing extras
  • Shorthand { key: } binds same-named variables
  • **nil forbids extra keys; patterns nest deeply
case { id: 1, items: [9, 8] }
in { items: [head, *] } then puts head
end

Frequently asked questions

Is the “Array and Hash Patterns” lesson free?

Yes — the full text of “Array and Hash Patterns” is free to read here on the web, and the Ruby Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Ruby Academy course, upgrade to CoddyKit PRO.

What will I learn in “Array and Hash Patterns”?

Destructure data. 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; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Array and Hash Patterns” 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