The difference between Enumerable#each
and Enumerable#map
is whether it returns the receiver or the mapped result. Getting back to the receiver is
I agree with what you said. Enumerable#each
simply returns the original object it was called on while Enumerable#map
sets the current element being iterated over to the return value of the block, and then returns a new object with those changes.
Since Enumerable#each
simply returns the original object itself, it can be very well preferred over the map
when it comes to cases where you need to simply iterate or traverse over elements.
In fact, Enumerable#each
is a simple and universal way of doing a traditional iterating for loop, and each is much preferred over for loops in Ruby.