liveBook · Manning

Push

As explained in The Well-Grounded Rubyist, Fourth Edition

Array#push is a Ruby Array method that appends one or more objects to the end of an array.

Ruby’s Array#push appends one or more objects to the end of an array. Starting with [1, 2, 3, 4], a.push(5) produces [1, 2, 3, 4, 5]. section 9.2.2

Related operations clarify when push is the better fit:

  • Use << to append one object with infix syntax; unlike push, it does not take multiple arguments here. section 9.2.2
  • Pair push with unshift, pop, and shift when manipulating array ends. section 9.2.2

Appending a batch

a = [1, 2, 3, 4, 5] a.push(6, 7, 8) # a is now [1, 2, 3, 4, 5, 6, 7, 8]

How The Well-Grounded Rubyist, Fourth Edition covers it

How deep it goes
Defines single- and multiple-argument use, contrasting push with << and related array-end methods.
Examples
Demonstrates appending one object and appending three objects to arrays containing integers.
What it assumes
Assumes familiarity with arrays and basic array construction.
What you can do after
Apply it via Ruby’s Array API to append one or several objects at an array’s end.
How it connects
Connects push with Array, unshift, pop, shift, and the << operator.

Also explained in

Compare how 4 books explain push →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Well-Grounded Rubyist, Fourth Edition. The section links point to the parts of the book it draws on.