What is the difference between map, each and select in Ruby?
Use each when you want to do something with every element and do not need a new list, map when you want a new array of transformed values, and select when you want a new array holding only the elements your block says yes to. Each returns the original array, map returns the block results, select returns the matching elements.
Cite
Poignant Guide Ruby Q&A, "What is the difference between map, each and select in Ruby?", https://poignantguide.net/answers/read/what-is-the-difference-between-map-each-and-select-in-ruby/, accessed 2026-11-19.- Odile MarchettiStaffStaff: Ruby basics, objects and the Poignant Guide ·
Try all three on the same array
A block is the chunk of code between
{ }ordo ... endthat Ruby runs once per element (if blocks are new to you, see what a block is and what yield does). Paste this into irb, Ruby's interactive prompt:nums = [1, 2, 3, 4] p nums.each { |n| n * 10 } # => [1, 2, 3, 4] p nums.map { |n| n * 10 } # => [10, 20, 30, 40] p nums.select { |n| n.even? } # => [2, 4]The same block shape,
{ |n| ... }, does three different jobs. The difference is what Ruby does with the value the block gives back. Thepmethod prints the inspected value of what it receives, which is why we can see the arrays. (The thread on puts, print and p covers that one.)What each method does with the block's value
- each throws the block's value away and returns the array it was called on. It exists for side effects such as printing or adding to a total.
- map (also spelled
collect) collects the block's value for every element into a new array of the same length. - select (also spelled
filter) asks the block a yes or no question. It keeps the element when the block returns something truthy, meaning anything exceptfalseandnil.
The Enumerable documentation describes them in those terms: map "returns objects returned by the block", select "returns elements selected by the block". Enumerable is a module (a bundle of methods) that arrays, hashes and ranges all share, so these methods work on all three.
A quick comparison
Method Block's job Returns Same length as input? eachdo something the original array yes, it is the same array mapproduce a new value array of block results always selectanswer yes or no array of matching elements only if all match filter_mapproduce a value, or nilto skiparray of truthy block results not usually filter_maparrived in Ruby 2.7 and is handy when you want to transform and filter in one step:p [1, 2, 3, 4].filter_map { |n| n * 10 if n.even? } # => [20, 40]Mistakes beginners make
Using each and expecting a new array. This runs without any error, and quietly gives you the wrong thing:
nums = [1, 2, 3] doubled = nums.each { |n| n * 2 } p doubled # => [1, 2, 3]The doubling happened, then Ruby discarded it. Change
eachtomapanddoubledbecomes[2, 4, 6].Using map when you meant select. Map keeps every element, so a yes or no block gives you a list of booleans:
p [1, 2, 3, 4].map { |n| n.even? } # => [false, true, false, true]Using select with a block that returns the value, not a test.
[1, 2, 3, 4].select { |n| n * 10 }returns all four numbers, because every number times ten is truthy. Select only looks at truthiness, never at the transformed value.Printing inside map.
map { |n| puts n }prints fine, but the new array is full ofnil, becauseputsreturnsnil. If you only want printing, useeach.Check this in order
- Do you want a new array at all? If not, use
each. - Do you want every element, changed? Use
map. - Do you want some of the elements, unchanged? Use
select(orrejectfor the opposite). - Do you want both? Try
filter_map, or chain:nums.select(&:even?).map { |n| n * 10 }. - Still surprised? Print the result with
pand look at its length.
Hashes work too
With a hash, the block receives the key and the value. Map still gives an array, but select keeps the hash shape:
h = { a: 1, b: 2, c: 3 } p h.map { |k, v| [k, v * 2] } # => [[:a, 2], [:b, 4], [:c, 6]] p h.select { |k, v| v > 1 } # => {:b=>2, :c=>3} p h.to_h { |k, v| [k, v * 2] } # => {:a=>2, :b=>4, :c=>6}The last line,
to_hwith a block, is the tidy way to get a hash back after transforming. These examples were run on Ruby 3.0 and later. The inspect format of hashes printed withpchanged in Ruby 3.4, so on that version you will see{b: 2, c: 3}instead.Chapter 4 of Why's (Poignant) Guide to Ruby on this site shows map, select and reject with a block, in the section on array methods. The Array documentation lists the full set, and Array includes Enumerable, so everything above applies. The method pages there are checked for Ruby 3.3.
Checked on 12 November 2026.
Write something first.
Give people something to work with: at least 30 words on what happened and what you tried.
That is too long. Keep it under 6,000 characters.
Write the question as the title, 15 to 140 characters, no links.
Pick a category.
Add a name (2 to 40 characters, no links).
That email address does not look right.
That was quick. Read the thread, then try again.
The form expired. Reload the page and post again.
Something went wrong with the form. Reload and try again.
Please complete the check and post again.
Limit reached for now. Try again later.
This thread is closed to new replies.
This thread no longer accepts replies.
Something went wrong with the form. Reload and try again.
Post a reply or question first (name and email), then this browser can vote, edit and accept answers.
You cannot vote on your own post.
Only the author (within 30 days) or the forum team can do that.
That email already has an account. Use your sign-in link instead.
New accounts are paused for the moment. Try again later.
That was already posted.