Skip to content

Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →

instance method

Array#*

(::string string_separator) -> ::String
(::int n) -> ::Array[Elem]
self * n → new_array
self * string_separator → new_string

When non-negative integer argument n is given, returns a new array built by concatenating n copies of self:

Example
a = ['x', 'y']
a * 3 # => ["x", "y", "x", "y", "x", "y"]

When string argument string_separator is given, equivalent to self.join(string_separator):

Example
[0, [0, 1], {foo: 0}] * ', ' # => "0, 0, 1, {foo: 0}"

Ruby version changes

  1. 3.0
See also #& #+ .try_convert #- also in 4.0 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0