DEV Community

Donna Hogan
Donna Hogan

Posted on • Originally published at Medium on

6 1

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.

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay