Skip to content

Maintenance Ruby 3.3 gets fixes, but no new features. Switch to Ruby 4.0 →

instance method

Array#push

array.push(*objects) → self

Appends trailing elements.

Appends each argument in objects to self; returns self:

Example
a = [:foo, 'bar', 2]
a.push(:baz, :bat) # => [:foo, "bar", 2, :baz, :bat]

Appends each argument as one element, even if it is another Array:

Example
a = [:foo, 'bar', 2]
a1 = a.push([:baz, :bat], [:bam, :bad])
a1 # => [:foo, "bar", 2, [:baz, :bat], [:bam, :bad]]

Related: pop, shift, unshift.

See also #product #rassoc #prepend #reject also in 4.1 · 4.0 · 3.4 · 3.2 · 3.1 · 3.0