0
0
RubyComparisonBeginner · 3 min read

Map vs Each in Ruby: Key Differences and When to Use Each

map transforms elements in a collection and returns a new array with the results, while each simply iterates over elements for side effects and returns the original collection. Use map when you want a new array of changed values, and each when you want to perform actions without changing the collection.
⚖️

Quick Comparison

Here is a quick side-by-side comparison of map and each in Ruby.

Aspectmapeach
PurposeTransforms elements and returns a new arrayIterates elements for side effects, returns original collection
Return valueNew array with transformed elementsOriginal array unchanged
Typical use caseWhen you want to change or collect resultsWhen you want to perform actions like printing or modifying external state
Does it modify original array?No, unless using map!No, unless elements are changed inside block
Chainable?Yes, returns new arrayYes, returns original array
Common alternative namecollectNo common alternative
⚖️

Key Differences

map and each are both methods to loop over collections like arrays, but they serve different purposes. map applies the block to each element and collects the results into a new array, leaving the original array unchanged. This makes map perfect when you want to transform data.

On the other hand, each simply runs the block for each element without collecting results. It returns the original array, making it useful for performing actions like printing or updating external variables without changing the array itself.

While map focuses on producing a new array of transformed values, each focuses on side effects. Also, map is chainable with other enumerable methods because it returns a new array, whereas each returns the original array, which may or may not be useful for chaining depending on your goal.

⚖️

Code Comparison

Here is how you use map to create a new array with each number doubled.

ruby
numbers = [1, 2, 3, 4]
doubled = numbers.map { |n| n * 2 }
puts doubled.inspect
Output
[2, 4, 6, 8]
↔️

Each Equivalent

Using each to double numbers requires an external array to store results because each does not collect return values.

ruby
numbers = [1, 2, 3, 4]
doubled = []
numbers.each { |n| doubled << n * 2 }
puts doubled.inspect
Output
[2, 4, 6, 8]
🎯

When to Use Which

Choose map when you want to transform each element and get a new array with those transformed values. It is concise and clear for data transformation tasks.

Choose each when you want to perform actions for each element without changing the collection, such as printing, logging, or modifying external variables. It is best for side effects rather than data transformation.

Key Takeaways

map returns a new array with transformed elements; each returns the original array.
Use map for data transformation and collecting results.
Use each for performing actions with side effects without changing the array.
map is chainable and functional; each is procedural and used for iteration.
To mimic map with each, you must manually collect results.