# What is the difference between map, each and select in Ruby?

Source: https://poignantguide.net/answers/read/what-is-the-difference-between-map-each-and-select-in-ruby/
Site: Poignant Guide Ruby Q&A, category Arrays, hashes and Enumerable
Published: 2026-11-17. Updated: 2026-11-17. Replies: 0.

## Question by Odile Marchetti (staff), 2026-11-17

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.

## Try all three on the same array

A block is the chunk of code between `{ }` or `do ... end` that Ruby runs once per element (if blocks are new to you, see [what a block is and what yield does](/answers/read/what-is-a-block-in-ruby-and-what-does-yield-do/)). Paste this into irb, Ruby's interactive prompt:

```ruby
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. The `p` method prints the inspected value of what it receives, which is why we can see the arrays. (The thread on [puts, print and p](/answers/read/what-is-the-difference-between-puts-print-and-p-in-ruby/) 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 except `false` and `nil`.

The [Enumerable documentation](https://docs.ruby-lang.org/en/3.3/Enumerable.html) 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? |
|---|---|---|---|
| `each` | do something | the original array | yes, it is the same array |
| `map` | produce a new value | array of block results | always |
| `select` | answer yes or no | array of matching elements | only if all match |
| `filter_map` | produce a value, or `nil` to skip | array of truthy block results | not usually |

`filter_map` arrived in Ruby 2.7 and is handy when you want to transform and filter in one step:

```ruby
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:

```ruby
nums = [1, 2, 3]
doubled = nums.each { |n| n * 2 }
p doubled
# => [1, 2, 3]
```

The doubling happened, then Ruby discarded it. Change `each` to `map` and `doubled` becomes `[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:

```ruby
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 of `nil`, because `puts` returns `nil`. If you only want printing, use `each`.

## Check this in order

1. Do you want a new array at all? If not, use `each`.
2. Do you want every element, changed? Use `map`.
3. Do you want some of the elements, unchanged? Use `select` (or `reject` for the opposite).
4. Do you want both? Try `filter_map`, or chain: `nums.select(&:even?).map { |n| n * 10 }`.
5. Still surprised? Print the result with `p` and 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:

```ruby
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_h` with 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 with `p` changed 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](https://poignantguide.net/ruby/chapter-4.html) on this site shows map, select and reject with a block, in the section on array methods. The [Array documentation](https://docs.ruby-lang.org/en/3.3/Array.html) 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.

---
Cite as: 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.
