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#replace

(::array[Elem] other_array) -> self
replace(other_array) → self

Alias of #initialize_copy

Replaces the elements of self with the elements of other_array, which must be an array-convertible object; returns self:

Example
a = ['a', 'b', 'c']   # => ["a", "b", "c"]
a.replace(['d', 'e']) # => ["d", "e"]

Related: see Methods for Assigning.