What is the difference between .map and .map!


#1


https://www.codecademy.com/en/courses/ruby-beginner-en-L3ZCI/1/6?curriculum_id=5059f8619189a5000201fbcb


numbers_array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

strings_array = numbers_array.map(&:to_s)


#2

The above will create an array of strings:

["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"]

We had to store it in another variable, though.

This,

numbers_array.map!(&:to_s)

will map the array onto itself so it is now,

["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"]

This,

numbers_array.map!(&:to_i)

will map on itself,again, this time numbers...

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

#3

Methods with a ! modify the object that they are called on (this is by convention, something generally agreed upon)

In more detail:
http://docs.ruby-lang.org/en/trunk/syntax/methods_rdoc.html#label-Method+Names