0

Let's say I have following block of code:

arr = ['a','b','c']
arr.map {|item| item <<'1'} #=> ['a1','b1','c1']
arr #=> ['a1','b1','c1']

Why does Array#map change the array? It should only create a new one. When I'm using + in the block instead of <<, it works as expected. Does Array#each change the array itself, or does it only iterate over it and return itself?

2 Answers 2

4

My question is: Why does map change array? it should only create new.

map doesn't change the Array. But << changes the Strings in the Array.

See the documentation for String#<<:

str << obj → str

Append—Concatenates the given object to str.

Although it isn't mentioned explicitly, the code example clearly shows that << mutates its receiver:

a = "hello "
a << "world"   #=> "hello world"
a.concat(33)   #=> "hello world!"

It's strange, because when I'm using + operator in the block insted of << it works as expected.

+ doesn't change the Strings in the Array.

See the documentation for String#+:

str + other_str → new_str

Concatenation—Returns a new String containing other_str concatenated to str.

Note how it says "new String" and also the return value is given as new_str.

And my second question: Does Array#each change array itself or it only iterate over array and return itself?

Array#each does not change the Array. But of course, the block passed to Array#each may or may not change individual elements of the Array:

arr = %w[a b c]
arr.map(&:object_id)          #=> an array of three large numbers
arr.each {|item| item <<'1' } #=> ['a1', 'b1', 'c1']
arr.map(&:object_id)          #=> an array of the same three large numbers

As you can see, Array#each did not change the Array: it is still the same Array with the same three elements.

Sign up to request clarification or add additional context in comments.

Comments

2

Using map or each makes difference on the outer array (map will return a new array, each will return the original array), but it will not make difference on what strings the array contains; in either case, the strings contained in the array will be the original strings modified.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.