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.
Only the Array
class redefines the method. ⚠️ Beware of commutativity.
When the contents of the array are repeated, the instances are not duplicated. ⚠️ Beware of mutation.
When creating a String
, the *
operator is in fact the equivalent of the join
method. The method handles array nesting. Do you know how?
Link to Ruby documentation for Array#*
.