Learn Ruby

select to select certain items from array

We have an array of numbers and we want to get only the even numbers from that array. We can do something like this.

Above code works but Array provides select to handle such cases in a much better way. select will only select items for which the result of condition is true. Let’s see it in action.

As we can see the code is much cleaner compared to the previous one.