The .select Method on an Array in Ruby

Boris Paskhaver
A free video tutorial from Boris Paskhaver
Software Engineer | Consultant | Author
4.7 instructor rating • 6 courses • 292,691 students

Lecture description

  • Call the .select method on an array to return an array of elements for which the block condition evaluates to true.

Learn more from the full course

Learn to Code with Ruby

A comprehensive introduction to coding with the Ruby programming language. Complete beginners welcome!

31:25:46 of on-demand video • Updated September 2020

  • Learn to program in the Ruby programming language
  • Master common Ruby objects like Strings, Arrays, Hashes, and more
  • Solve popular coding challenges and algorithms with Ruby
  • Dive into object-oriented programming including classes and more
English [Auto] Are right in this lesson we'll take a look at the Select method on an array and what the select method does is extract only those elements that fit a certain condition or criteria. It selects those elements that you are looking for. For example let's say I have an array called grades and it's going to be filled with numeric values let's say 80 95. Thirteen seventy six. Let's do twenty eight and let's do thirty nine. All right. Bunch of random numbers within grades. What if I wanted to select only the numbers in here that were greater than let's say 75. Now I could do something like Do an each loop and then whenever a condition is met with something like. If I could add that element to a new array. But that would be a little bit of a non efficient way to do it it would be a little bit long winded. There is a more optimal method in Ruby called select. So how to select work. Well I can call the Select method on my array and just like each select is going to take a block. I can do that here with and do. And after my do I can define a block variable and again just like with each. This is going to represent every single element within my array. So I want to give each element the name of number. Now unlike each where we specify what we want to do or what we want to do with the specific block variable whenever using the Select method whatever is in the block must evaluate to a boolean value. So whatever we write in here in the body must evaluate to either a true or a false. So what do we want to do. We want to check if the number is let's say greater than or equal to 75. This is always going to evaluate to a boolean. We're going to take every single number here iterate over them one by one and compare its value to seventy five. What's going to happen here predictively is Ruby is only going to select those values for which this condition evaluates to true. So whenever the number is greater than or equal to 75 Ruby is going to select it whenever it's not. Ruby is going to reject it and it's going to give us back an array of only those values that have that condition fit. So if I let's say create a variable here call matches. I'm going to assign it to this entire operation. If I output matches immediately below match's is going to be a brand new array. But you can see it's only going to contain the elements where the number is greater than or equal to 75. We have used our original grades right here and we have selected those numbers that are greater than or equal to 75. This is the resulting array 80 95 and 76 fit that criteria 13 28 and 39 do not. So they have not been included. All right. So this is the exact same thing as if we were to create an empty array here called matches and simply iterate over grades with each and just push each valid value to matches. This is just a little bit more optimal. A little bit cleaner. All right. Now what we have in here can be anything that results in a boolean value it can be something like a greater than comparison a less than comparison. It can be an equality comparison. It can be an inequality comparison or of course what it can be is some kind of predicate method or a boolean method because remember those methods return valid boolean values true or false. So what I can do here is instead of doing something like this I can call a method like. Even on every single number that I'm iterating over. Remember that even boolean method with that question mark at the end returns a true if the number is even a false. Otherwise this is going to evaluate to a boolean which means it's a perfectly valid thing to put in the body of a block that follows the slack method. So now if I output matches Valo we're going to get just the even numbers 1876 and 28. We've now rejected 95 13 and 39 because those are not even the interior condition. Here when Ruby was iterating over those evaluated to false instead of true you could even use this for more complex operations. For example let's replace all of this with a new array. I'm going to call it words. And these are going to be words that could be potentially palindromes a palindrome is a word that is the same backwards as it is forwards. So for example level is a palindrome. And let's say I do something here like selfless which looks like a palindrome. Let's say I do race car and let's say something different that isn't like dinosaurs. All right. Now imagine we have an array of a thousand dollars and we want to select all of those that are palindromes so I can define a variable called palindromes equals and I want to select something that fits a criteria and the criteria is that the word is the same spelled forward as it is backward. Well how can I do this. Well I can iterate over words with the Select method. This time I'm going to use the block design with the curly braces after I buy myself some space here so I can open up my curly braces in my block parameters or block variables. I can define a word that's going to represent every single element that we iterate over in the words array. And what do I want to check. I want to check if that word is the same as that word reversed. Remember that the reverse method is available on a string and it's going to return that string in reverse order. So once we get that we simply compare that to the original string. And here because we're using the equality operator the double equal sign. We're always going to get back either a true or a false a valid boolean value. Remember whenever you using the Select method unlike each whatever is inside the block must evaluate to a true or false. In this case we are we do have a condition that this is always going to give us a true or a false and if it's true then it's going to select it and store it in palindromes. And if not it's going to discard it. So now if I take a look at palindromes after the matter we can see that it has in fact selected the palindromes level is level spelled backward and racecar is racecars spelled backwards. So you can see this operation is applicable to an array of any kind of object we can use on an array of strings. We can use that on an array of numbers or floats whatever you'd like. The only important thing here is simply calling the Select method and remember to put it that block and again within the block. Unlike with the each method the block for a select method must have some kind of condition or evaluation that Idalia waits to True or False whenever it's true. Ruby is going to take those values and store them inside an array. For example here we have the power drone array whenever the condition is false. Ruby is simply going to ignore them. We are selecting those elements from the original array that match a condition. That's why it's called the Select method and in the next lesson it will take a look at the complimentary method to this. Which is reject.