DEV Community

Donna Hogan
Donna Hogan

Posted on • Originally published at Medium on

Quick reference for ruby array methods: push/pop vs. shift/unshift

I love to make tables to help me integrate ideas or objects and how they relate to each other.

When I was learning how to work with arrays in Ruby, the words for functions .push, .pop, .shift, and .unshift, didn’t carry any intrinsic meaning or distinction to me in terms of working with an array (unlike .collect, and .size, which do). I kept getting confused about which thing returns an element, and which one returns the rest of the array, and whether it acts on the beginning or the end. So, here’s my table!

beginning end
adding, returns array .unshift .push
returns removed element .shift .pop
  • and of course, << (shovel) is syntactic sugar for .push

Originally published at donnacodes.com on September 11, 2017.

Top comments (0)