DEV Community

loading...

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

ddhogan profile image Donna Hogan Originally published at Medium on ・1 min read

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.

Discussion

pic
Editor guide