.map VS .select in Ruby

A Close Look At Their Differences

Travis Prol
3 min readJun 17, 2020

While working my Module 1 Final Project for Flatiron School’s Software Engineering Bootcamp, I was making errors by using the .map and .select methods at the wrong time. While these two methods are similiar, they work very differently.

We will be looking at the same Dog class and instances while we look at both .map, and .select.

class Dog
attr_accessor :name, :owner
@@all = []
def initialize(name, owner)
@name = name
@owner = owner
@@all << self
end
def self.all
@@all
end
end

Here we are creating a Dog class, initializing a new dog with a name and owner, then storing each new instance of the Dog class into an empty array.

Now, let’s make some dogs! We are going to have five dogs to work with moving forward:

        #name    #owner
Dog.new("Bella", "Corey")
Dog.new("Pippa", "Jeff")
Dog.new("Archie", "Christiana")
Dog.new("Harley", "Corey")
Dog.new("JoJo", "Travis")

Since we added each dog to an array with the self.all method, we can look at all of our dogs by calling Dog.all. This will look something like this (note that object ID’s are unique and will not match if you are coding along):

Dog.all[#<Dog:0x00007ffc2b8843c8 @name="Bella", @owner="Corey">, #<Dog:0x00007ffc2b89fbf0 @name="Pippa", @owner="Jeffrey">, #<Dog:0x00007ffc30086838 @name="Archie", @owner="Christiana">, #<Dog:0x00007ffc30091850 @name="Harley", @owner="Corey">, #<Dog:0x00007ffc3100caa0 @name="JoJo", @owner="Travis">]

.map

.map is a method that we can call on any array. Since all of our dogs are in an array we can use the .map method to pull certain information out:

Dog.all.map{|dog| dog.name}

If you were to read this in plain English, it would read something like, “look at every dog, and map through it to pull out the name value of each dog.” This would return:

Bella
Pippa
Archie
Harley
JoJo

Similarly, we can do the same with the owners:

Dog.all.map{|dog| dog.owner}

This returns:

Corey
Jeffrey
Christiana
Corey
Travis

Notice that Corey’s name appears twice. This is because Corey is the owner of of both Bella and Harley. .map pulls out every value, regardless of whether or not it is unique.

.select

.select is also a method that we can call on an array. While .map pulls out the value that we ask it to, .select returns the objects that fulfill our condition:

Dog.all.select{|dog| dog.name}

Reading in plain English, “look at every dog, and select the dog objects that have a name.” This would return the actual dog objects, not their name:

#<Dog:0x00007fe9ad0225d0>
#<Dog:0x00007fe9ad022530>
#<Dog:0x00007fe9ad022418>
#<Dog:0x00007fe9ad022378>
#<Dog:0x00007fe9ad022288>

At first glance, .select seems slightly useless. There are, however, instances where there is no other way to get to your answer other than to use it.

Combining .map And .select

Say we wanted to find out all the names of Corey’s dogs. We would first use .select to get all of the objects whose owner is Corey. We are going to set it equal to a variable so that we can access it later:

coreys_dogs_array = Dog.all.select{|dog| dog.owner == "Corey"}

In plain English, “look at every dog, and select the dog objects whose owner is Corey, then set it equal to the variable coreys_dog_array.

This returns:

#<Dog:0x00007fceba00e660>
#<Dog:0x00007fceba00e480>

Again, doesn’t look like we can do much with that, but since we saved this to a variable, we can now use .map to pull out the names of these two dogs:

coreys_dogs_array.map{|dog| dog.name}

In plain English, “look at every dog, and return the value of name for each one.”

This returns:

Bella
Harley

.map and .select are two very useful tools in the Ruby language that can be combined to access the most specific data.

--

--

Travis Prol

Full Stack Web Developer 🧑🏻‍💻 Artists Who Code 🤓 Lets go Yanks ⚾️