Ruby: The Different Between Each, Map and Collect

Posted on

When I was learning Ruby, I found that each, map and collect look alike but they aren't. Let's see what the different between them is. I will use irb to demonstrate it.


Each basically iterate each item and performs the block statement.

> [1, 2, 3].each {|item| puts item * 2}
=> [1, 2, 3]

In above code, we have an array [1, 2, 3] then each iterate the item in array and print out the the result of item * 2. Each doesn't return anything.


Map is different with each. Map will create a new array as return value after performing the block for each item.

> [1, 2, 3].map {|item| item * 2}
[2, 4, 6]

[2, 4, 6] is new array after calling map and performing item * 2.


Despite different name, collect functionality is same as map but map is more popular and preferable to use.

> [1, 2, 3].collect {|item| item `* 2}
[2, 4, 6]

Prefer map over collect, find over detect, select over find_all, reduce over inject and size over length. This is not a hard requirement; if the use of the alias enhances readability, it's ok to use it. source: Ruby style guide