Array and Hash Patterns
Destructure data.
Array and Hash Patterns is a free Ruby Academy lesson on CoddyKit. This is lesson 2 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.
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'
endBinding Array Elements
Use identifiers to capture each position into a variable.
case [10, 20]
in [x, y]
puts(x + y)
endLength 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'
endThe 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
endSplat 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}")
endHash 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'
endBinding 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}")
endShorthand Key Binding
Omitting the pattern after a key binds a variable named after the key automatically.
case { city: 'Istanbul' }
in { city: }
puts(city)
endRequiring 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'
endNested 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}")
endMatching 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)
endQuick 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
*restcaptures remaining elements- Hash patterns match by key, allowing extras
- Shorthand
{ key: }binds same-named variables **nilforbids extra keys; patterns nest deeply
case { id: 1, items: [9, 8] }
in { items: [head, *] } then puts head
endLearn 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 “Array and Hash Patterns” lesson free?
Yes — any 3 lessons of the Ruby Academy learning path, including “Array and Hash Patterns”, 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 “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, so you can start here or from the beginning and move at your own pace. This is lesson 2 of 4.
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
- case/in Basics
- Array and Hash Patterns
- Find Patterns and Guards
- Practical Use Cases