liveBook · Manning

Plus operator

As explained in The Well-Grounded Rubyist, Fourth Edition

The Array#+ method is a Ruby method that returns a new array containing the elements of its operand arrays without mutating the receiver.

Array#+ combines the receiver array with an argument array and returns a new array, leaving the receiver unchanged. Unlike concat, it does not modify the receiver. section 9.2.3

>> a = [1,2,3]
=> [1, 2, 3]
>> b = a + [4,5,6]
=> [1, 2, 3, 4, 5, 6]
>> a
=> [1, 2, 3]              1

Combining two arrays leaves a unchanged while assigning the combined result to b. section 9.2.3

How The Well-Grounded Rubyist, Fourth Edition covers it

How deep it goes
Demonstrates Array#+ with an irb example and contrasts its behavior with concat.
Examples
Combines [1, 2, 3] and [4, 5, 6], then checks that the original array is unchanged.
What you can do after
Apply Array#+ to combine arrays while keeping the receiver unchanged.
How it connects
Connects array combination to concat, push, and Ruby’s method-call model.

Also explained in

Compare how 2 books explain plus operator →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Well-Grounded Rubyist, Fourth Edition. The section links point to the parts of the book it draws on.