0019 Array_multiplication

The Array class redefines the `*` operator

The * operator does two different things:

  • Repeat the contents of the array a given number of times.
  • Create a String with the elements separated by the given delimiter.

0019-array_multiplication_1.png

Only the Array class redefines the method. ⚠️ Beware of commutativity.

0019-array_multiplication_2.png

When the contents of the array are repeated, the instances are not duplicated. ⚠️ Beware of mutation.

0019-array_multiplication_3.png

When creating a String, the * operator is in fact the equivalent of the join method. The method handles array nesting. Do you know how?

0019-array_multiplication_4.png

Link to Ruby documentation for Array#*.