0Pricing
Ruby Academy · Lesson

Array and Hash Patterns

Destructure data.

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

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