95

I am having a hard time understanding what the shift and unshift methods of the Array class do in Ruby. Can somebody help me understand what they do?

4
  • 181
    Once you learn that shift/unshift are like push/pop on the other end of the array, you can mentally drop the 'f' from the name of the methods to remember which one 'dumps' elements and which one 'inserts' them. :)
    – Phrogz
    Jan 21, 2011 at 17:37
  • One of the best comments ever! Thanks you so much :)
    – tvdeyen
    Nov 6, 2012 at 13:14
  • 18
    @Phrogz Actually, vertebrate digestive systems are better modeled as queues than as stacks.
    – Jian
    May 2, 2013 at 1:59
  • 8
    @Jian :) Right: push it into the top, and shift it out the other end.
    – Phrogz
    May 2, 2013 at 4:06

5 Answers 5

105

Looking at the Ruby Documentation

Array.shift removes the first element from the array and returns it

a = [1,2,3] 
puts a.shift
 => 1 
puts a
 => [2, 3] 

Unshift prepends the provided value to the front of the array, moving all other elements up one

a=%w[b c d]
 => ["b", "c", "d"] 
a.unshift("a")
 => ["a", "b", "c", "d"] 
5
  • 9
    You can basically think of shift and unshift as being operations on a FIFO queue Sep 16, 2010 at 9:13
  • @JacoPretorius Huh? shift and unshift act like a FILO stack, push and shift would act like a FIFO queue, unless I am confused about something. Dec 27, 2017 at 19:37
  • @GregSchmit Ah, you're right. Or actually, a LIFO queue, right? Dec 28, 2017 at 21:42
  • @JacoPretorius Well I think LIFO == FILO (first in last out implies that the last one in must come out first if the pattern holds). Dec 28, 2017 at 21:44
  • @JacoPretorius Both LIFO and FILO mean "behave like a stack". Dec 28, 2017 at 21:44
59

shift and unshift acts in similar way as pop and push: they are meant to use arrays as stacks to which you can append and remove elements (usually one per time). The difference is just that shift and unshift add/remove elements at the beginning of an Array, actually shifting all other elements, while pop and push add/remove elements at the end of the Array, so preserving other elements' indices.

Examples:

                      # Spacing for clarity:
a = [2, 4, 8]    # a =>       [2, 4, 8]
a.push(16, 32)   # a =>       [2, 4, 8, 16, 32]
a.unshift(0, 1)  # a => [0, 1, 2, 4, 8, 16, 32]
a.shift          # a =>    [1, 2, 4, 8, 16, 32]
a.pop            # a =>    [1, 2, 4, 8, 16]
3
  • If you were to edit your answer to summarize the the mipadi thread, I would be glad to upvote. Sep 15, 2010 at 14:53
  • Cool. Also, I'm not very knowledgeable about Ruby, but if it runs on the JVM then I would expect that push/pop would be faster, as it doesn't have to move all those elements over. Sep 15, 2010 at 15:52
  • Upvote for stack analogy, but think of pipes too. As we are supposed to be programmers we should also be thinking shift is a shift-left and unshift is a shift-right on a horizontal left to right array.
    – mckenzm
    Jul 27, 2016 at 23:08
9

It grabs the first element, removes it from the array, and returns the removed element. It's basically a way to treat an array like a stack: shift is pop, unshift is push.

11
  • 8
    Well shift and unshift are similar to pop and push, except they add and remove stuff from the beginning of an array, instead from the end. Sep 15, 2010 at 14:36
  • 2
    This answer is at precisely the correct level of abstraction. Sep 15, 2010 at 14:37
  • @Alberto: Or, in other words, they consider the front to be the top. There's no requirement for it to be otherwise. Sep 15, 2010 at 14:37
  • 5
    I was just pointing out that, since pop and push are also Array method, confusion is not to be made. :-) Sep 15, 2010 at 14:43
  • 3
    @Alberto: That's actually a good point. The shift/unshift methods use the front as top while the push/pop methods use the end as top. They both treat the array as a stack, differing only in which end they use. Sep 15, 2010 at 14:52
2

If you can think of the array as being like a queue of values to be processed, then you can take the next (front) value and "shift" the other valuess over to occupy the space made available. unshift puts values back in - maybe you're not ready to process some of them, or will let some later code handle them.

1

It returns the first element of the array, and removes it from the array, shifting the elements back one place.

So shifting [1,2,3,4,5]

returns 1, and sets the array to be [2,3,4,5].

More here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.