How to Use the flat_map Method in Ruby

As a Ruby developer, you have probably used the map method to transform arrays and other collections of data. But have you ever needed to transform an array and flatten the result in one go? This is where the flat_map method comes in.

The flat_map method is a combination of the map and flatten methods. It takes an array, performs a transformation on each element of the array, and then flattens the resulting array. Let’s take a look at some examples to see how it works.

Example 1: Retrieving Data from Nested Hashes

Suppose you have a hash containing information about people and their pets, where each person may have multiple pets. You want to retrieve a list of all pets with the name of the owner. Here’s how you could use flat_map to achieve this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
people_and_pets = {
  'Alice' => ['Fluffy', 'Fido'],
  'Bob' => ['Whiskers'],
  'Charlie' => ['Buddy', 'Daisy', 'Rover']
}

pets_with_owners = people_and_pets.flat_map do |owner, pets|
  pets.map { |pet| { 'name' => pet, 'owner' => owner } }
end

puts pets_with_owners

# Output:
# {"name"=>"Fluffy", "owner"=>"Alice"},
# {"name"=>"Fido", "owner"=>"Alice"},
# {"name"=>"Whiskers", "owner"=>"Bob"},
# {"name"=>"Buddy", "owner"=>"Charlie"},
# {"name"=>"Daisy", "owner"=>"Charlie"},
# {"name"=>"Rover", "owner"=>"Charlie"}

In this example, the flat_map method is used to transform the nested hash into an array of pet objects, where each object contains the name of the pet and the name of its owner. The flat_map method flattens the resulting array, so that it’s a single array of pet objects.

Example 2: Exploding Words in a String

Suppose you have a string containing words separated by spaces, and you want to “explode” the string into an array of individual letters. Here’s how you could use flat_map to achieve this:

1
2
3
4
5
6
string = "The quick brown fox"
letters = string.split.flat_map { |word| word.split('') }
puts letters.inspect

# Output:
# ["T", "h", "e", "q", "u", "i", "c", "k", "b", "r", "o", "w", "n", "f", "o", "x"]

In this example, the split method is used to break the string into an array of words. The flat_map method is then used to transform each word into an array of letters, and the resulting array is flattened into a single array of letters.

Example 3: Filtering Arrays with Regular Expressions

Suppose you have an array of strings, and you want to select only the strings that contain a certain pattern. Here’s how you could use flat_map with a regular expression to achieve this:

1
2
3
4
5
6
7
8
words = ["apple", "banana", "orange", "grape", "watermelon"]
pattern = /an/

selected_words = words.flat_map { |word| word.scan(pattern) }
puts selected_words.inspect

# Output:
# ["a", "n", "a", "n", "a"]

In this example, the scan method is used with a regular expression to find all occurrences of the pattern “an” in each word. The flat_map method is then used to flatten the resulting arrays into a single array of matches.

Conclusion

The flat_map method is a versatile and powerful tool that can be used to transform and flatten arrays and other collections of data in Ruby. By combining the map and flatten methods, flat_map can simplify your code and make it more concise.