Using each, collect and select to pull items from an array in Ruby
A guide for beginner developers.
Imagine that we have an fruit basket and we only want to pull out apples.
How would we go about doing this in Ruby if our fruit basket was an array and our fruits were strings? Let’s examine a few different ways. Here is our fruit basket.
fruit_basket = [‘apple’, ‘banana’, ‘apple’, ‘apple’, ‘pear’, ‘orange’]When I was a coding baby, I learned about each. I loved each so much. Every problem looked like a nail and each was my beautiful, shiny hammer.

Back then, I would have solved the apples problem with code that looked this.
def get_apples_with_each(fruit_basket)apple_basket = []fruit_basket.each do |food|
if food == ‘apple’
apple_basket << food
end
endreturn apple_basketend
This code starts by forming a new array. It then iterates over the array with each, selecting each string that starts with an apple. Apple items will be shoveled into the new array as we go.
Why do we need the empty array? We can’t we just return the each statement? It’s because each has a return value of the original array that it was passed.
Later in my career, I realized that I could avoid the empty array by using collect, which, unlike each, returns a new array.
Sidenote: collect is exactly the same as map . Nowadays I am more inclined to use map than collect, just because map is shorter and Programmers Are Lazy, but it really doesn’t matter. When I was a beginner I preferred typing collect because that’s how I learned it first. Anyway, let’s look at our new method.
def get_apples_with_collect(fruit_basket)fruit_basket.collect do |food|
if food == ‘apple’
food
end
endend
Woah, it’s so much simpler now! But what are we getting returned?
[“apple”, nil, “apple”, “apple”, nil, nil]What’s a nil? I can’t eat a nil. I bet it tastes terrible.
The problem is that collect’s return array must accept a value on each iteration and in cases where the fruit is not an apple, it’s instead placing nil.
We can clean up this result with a cute little method called compact. Compact removes all nils from an array. Compact is perfect for this circumstance!
def get_apples_with_collect_compacted(fruit_basket)result = fruit_basket.collect do |food|
if food == ‘apple’
food
end
endreturn result.compactend
Great! That returns what we want and it’s still cleaner than using each. I bet we could simplify this process even more, though… (P.A.L., after all).
As it turns out, Ruby contains a built-in method for finding and pulling appropriate items out of an array. This method is called select.
def select_apples(fruit_basket) fruit_basket.select{|food| food == ‘apple’}end
HOLY APPLE PIE!
That’s just one line of code!
However, to our beginner eyes this looks a little bit intimidating.
Let’s break down the functionality of select.
Like collect and each, select iterates over an array. select also accepts a block. Every time the contents of the block are true, it adds the enum value (the value currently being looked at) to its return array.
Nowadays, when it comes to pulling items out of an array, I’m more likely to use select than each or collect. That said, each and collect are powerful tools with their own proper uses. As we grow as developers, we add new tools to our toolbox, but rarely do we abandon old tools. In other words, someday your hammer will look like this!

The Difference Between find_all and select
If you’ve gotten this far and you have some knowledge of Ruby, you may be wondering why I used select instead of find_all. The truth is, when you’re working with an array, it doesn’t matter.
However, when working with hashes, select will return a hash while find_all will return an array. Just a little bonus to keep in mind!
