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.
| Aspect | map | each |
|---|---|---|
| Purpose | Transforms elements and returns a new array | Iterates elements for side effects, returns original collection |
| Return value | New array with transformed elements | Original array unchanged |
| Typical use case | When you want to change or collect results | When 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 array | Yes, returns original array |
| Common alternative name | collect | No 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.
numbers = [1, 2, 3, 4] doubled = numbers.map { |n| n * 2 } puts doubled.inspect
Each Equivalent
Using each to double numbers requires an external array to store results because each does not collect return values.
numbers = [1, 2, 3, 4] doubled = [] numbers.each { |n| doubled << n * 2 } puts doubled.inspect
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.map for data transformation and collecting results.each for performing actions with side effects without changing the array.map is chainable and functional; each is procedural and used for iteration.map with each, you must manually collect results.