Wilbur Suero - Software Craftsman

Concise Data Extraction with Pattern Matching in Ruby

April 14, 2023

When working with collections of structured data in Ruby, such as arrays of hashes, we often need to extract or manipulate specific fields from each element in the collection. Traditionally, this is done using iterative loops and if statements, which can become verbose and hard to read, especially if the collection is large or complex.

This is where pattern matching can help. Pattern matching is a powerful feature that allows you to match and extract specific fields from structured data with ease. Ruby 2.7 introduced a new pattern matching syntax that makes it even easier to use.

Here’s an example of how you can use pattern matching to extract fields from an array of user profiles:

people = [
  { name: "John", age: 30, gender: "male" },
  { name: "Jane", age: 25, gender: "female" },
  { name: "Jack", age: 40, gender: "male" }
]

people.each do |person|
  case person
  in { name: name, age: age, gender: gender }
    puts "#{name} is a #{age}-year-old #{gender}."
  end
end

In this example, we’re using a case statement with a pattern that matches against each element in the people array. The pattern specifies that we want to extract the name, age, and gender keys from each hash.

If the pattern matches, we’ll execute the code inside the in block, which prints out a string using the extracted values for each element in the array.

Here’s another example. Let’s say you have an array of structured data that represents colors:

colors = [
  { red: 255, green: 0, blue: 0 },
  { red: 0, green: 255, blue: 0 },
  { red: 0, green: 0, blue: 255 }
]

colors.each do |color|
  case color
  in { red: 255, green: 0, blue: 0 }
    puts "The color is red."
  in { red: 0, green: 255, blue: 0 }
    puts "The color is green."
  in { red: 0, green: 0, blue: 255 }
    puts "The color is blue."
  else
    puts "The color is not primary."
  end
end

In this example, we’re using a case statement with patterns that match against each element in the colors array. The patterns specify that we want the red, green, and blue keys to have specific values that correspond to the primary colors.

If the pattern matches, we’ll execute the code inside the in block, which prints out a string indicating the color. Otherwise, we’ll execute the code inside the else block, which prints out a string indicating that the color is not primary.

Overall, pattern matching can make your code more concise and expressive when working with collections of structured data. By using patterns to extract specific values from each element in the collection, you can avoid verbose if statements and make your code easier to read and maintain.

If you’re working with collections of structured data in Ruby, pattern matching is a powerful tool to have in your arsenal. Give it a try and see how it can help you write more concise and expressive code!


Crafted by Wilbur Suero, a Software Engineer, who is passionate about building innovative and impactful solutions that drive business growth and operational excellence.