If you want a method that collects an array without modifying it, you can use map
, and you'll have something that works the same as each
. For example, you could do this:
array.each do |x|
x += 10
print "#{x}"
end
but you could just as easily do this:
array.map{|x| print (x + 10).to_s}
and it would have the exact same result. While each
can only do that, map
can alter its function using the !
, so I don't see why I would use each
anymore. Could you explain why I should ever use each
instead of map
if map
seems more versatile?