The side effects are the same which is adding some confusion to your reverse engineering.
Yes, both iterate over the array (actually, over anything that mixes in Enumerable) but map will return an Array composed of the block results while each will just return the original Array.
The return value of each is just the original array and is rarely used in Ruby code but map is one of the most important functional tools.
What map
does is return an array which contains the results of the block or named method that is passed. For example:
2.2.3 :001 > [:how, :now, :brown, :cow].map &:to_s
=> ["how", "now", "brown", "cow"]
In this case I didn't pass a block but just a Symbol
, however class Symbol
objects have a to_proc
method which will result in:
[:how.to_s, :now.to_s, ...]
BTW, you may be having a hard time finding the documentation because map is a method in Enumerable while each (the one method required by the Enumerable module) is a method in Array.
As a trivia note: the map implementation is based on each.