liveBook · Manning

Pop

As explained in The Well-Grounded Rubyist, Fourth Edition

Array#pop is a Ruby Array method that removes and returns one or more objects from the end of an array.

Array#pop removes objects from the end of an array and returns what it removed. With no argument, it removes one object; with an integer argument, it removes that many and returns them as an array. section 9.2.2

Remove the last element

a = [1, 2, 3, 4, 5] a.pop # => 5 a # => [1, 2, 3, 4] pop changes the array itself; the removed element is not merely captured. section 9.2.2

Use the argument form when several trailing elements belong together:

  • Contrast pop with shift: pop removes from the end, while shift removes from the beginning. section 9.2.2

How The Well-Grounded Rubyist, Fourth Edition covers it

How deep it goes
Defines single and counted removal, return shapes, mutation, and contrast with shift.
Examples
Uses integer arrays and word arrays to demonstrate single-element and multiple-element removal.
What it assumes
Assumes familiarity with arrays and introduces mutation through concrete retrieval and removal examples.
What you can do after
Apply it via Ruby's Array API to remove and capture one or several trailing elements.
How it connects
Connects Array with shift, push, and unshift through opposite-end insertion and removal operations.

Also explained in

Compare how 4 books explain pop →

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.