What is the difference between << and +=? What is the difference between << and +=? arrays arrays

What is the difference between << and +=?


They exhibit fairly different behaviors. One creates and assigns a new Array object, the other modifies an existing object.

+= would be the same as second_array = second_array + third_array. This sends the + message to the second_array object passing third_array as the argument.

Per the documentation Array.+ returns a new array object built by concatenating the two arrays. This will return a new object.

Array.<< simply push the parameter to the end of the existing array object:

second_array = []second_array.object_id = 1234second_array += [1,2,3,4]second_array.object_id = 5678second_array << 5second_array.object_id = 5678

There is also a difference in how the parameter is added. By adding other elements, it will help see why your arrays are not equal:

second_array = [1, 2, 3]# This will push the entire object, in this case an arraysecond_array << [1,2]# => [1, 2, 3, [1,2]]# Specifically appends the individual elements,# not the entire array objectsecond_array + [4, 5]# => [1, 2, 3, [1,2], 4, 5]

This is because Array.+ uses concatenation instead of pushing. Unlike Array.concat which modifies the existing object, Array.+ returns a new object.

You can think of a Ruby implementation like:

class Array  def +(other_arr)    dup.concat(other_arr)  endend

In your specific example, your objects look like this at the end:

first_array  = [[[1, 2, 3, 4, 5, 6, 7, 8, 9]]] # [] << [] << (1..9).to_asecond_array =  [[1, 2, 3, 4, 5, 6, 7, 8, 9]]  # [] + ([] << (1..9).to_a)third_array  =  [[1, 2, 3, 4, 5, 6, 7, 8, 9]]  # [] << (1..9).to_a


<< appends an item to an array

+= adds an array to an array.

Examples:

[1,2] << 3 # returns [1,2,3]

[1,2] += [3,4] # returns [1,2,3,4]


The last difference not mentioned so far between << and += is, that << is a method:

class Array  def << other    # performs self.push( other )  endend

whereas += is syntax:

a += b

and is merely a shorthand for writing:

a = a + b

So, in order to modify += behavior, one has to modify the + method:

class Array  def + other    # ....  endend