How to Use the Select Method in Ruby
The select method is a powerful method that automatically iterates through a collection in a Ruby program and extracts the values you want. Enumerators are a fun and very powerful way of working with collections in Ruby, and after you get used to implementing the methods I think you'll be shocked at how efficient it is integrate advanced functionality.
Guide Tasks
  • Read Tutorial
  • Watch Guide Video
Video locked
This video is viewable to users with a Bottega Bootcamp license

The select method is a powerful method that automatically iterates through a collection in a Ruby program and extracts the values you want to retrieve. Enumerators are a fun and powerful way of working with collections in Ruby, and after you get used to implementing the methods you'll be shocked at how efficient it is integrate advanced functionality with a limited amount of code.

Before going into the lesson, I'm going to ask you a question. Say, you are given an array of integers, and want to grab only the even integers. How can you do that in Ruby?

If you're coming from other programming languages, you would probably use a while loop to iterate through each element, and would check if that element meets the condition of being an even integer.

However in Ruby we don't have to go through all that trouble because we can use the select method.

To illustrate this functionality our code will look something like this:

(1...10).to_a.select do  |x|
  x.even?
end

If you run this code the output will be:

large

In the above code, we are converting a range of integers into an array, running it through the select method and displaying only the even values.

An even better way to do this would be putting this code on one line, like this:

(1...10).to_a.select  { |x| x.even?}

If you run this, it will give the same output as before.

But we're not done yet! Ruby gives us the ability to shorten this code even further, like this:

(1...10).to_a.select(&:even?)

Since you're likely to see all of these different options in a real world Ruby project, it's a good idea for you to learn various processes to achieve the same functionality.

If this code looks a little intimidating, don't let is scare you off. In this code, & is a shortcut that let's us avoid using a block variable. Whenever we use the & syntax Ruby knows that we want to apply whatever method we are calling to each element in the collection. In our example we're calling the even? method.

Since many professional Ruby developers use the shortcut syntax, you're likely to see the & code quite often in a production programs.

Let's now examine a few more examples of select:

Imagine that we have a sentence or an array of words and want to return only those words that have more than five letters. For this, we are going to use a new type of array syntax.

arr = %w(The quick brown fox jumped over the lazy dog)

Ruby converts this sentence into an array of words when you precede it with the %w symbol. If just run this code, your output should be:

["The", "quick", "brown", "fox", "jumped", "over", "the", "lazy", "dog"]

If you notice, the sentence is converted into an array of words.

Now let's see how we can select on our array to pull out the words with more than five characters:

arr = %w(The quick brown fox jumped over the lazy dog)
arr.select { |x| x.length > 5}

We cannot use the & block here because we are not calling a standalone method, rather we are running a comparison and this needs a variable.

In the above code, I'm checking if the length of each element in the collection is greater than five, and if so, I'm returning that element. So, my output should be:

large

There was only one word that had more than five letters, and that was displayed.

Before we end this lesson, let's look at one last example. Say, we have a sentence and want the program to return only the vowels. To do that, we can implement the following code:

%w(a b c d e f g).select { |v| v =~ /[aeiou]/ }

We'll be getting into how to use regular expressions in a later section, so for now, just examine what's going on here in terms of the functionality. Essentially, I'm asking Ruby to pick only the letters that match the vowel list enclosed inside my square brackets. To do this, Ruby will create an array of letters, iterate through each of them, store that item in the variable v and finally run a pattern recognition process to see if it matches with the list given inside the square brackets. If it matches, it returns that letter.

If you run this code, the output will be:

large

In this lesson, you learned three different ways to use the select method. I hope this gave you a glimpse into the power of select statement and functional programming in Ruby.