tags:

views:

83

answers:

5

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?

+3  A: 

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.

mipadi
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.
Alberto
This answer is at precisely the correct level of abstraction.
Steven Sudit
@Alberto: Or, in other words, they consider the front to be the top. There's no requirement for it to be otherwise.
Steven Sudit
@Alberto: The "beginning of the array" is akin to the top of the stack.
mipadi
I was just pointing out that, since `pop` and `push` are also `Array` method, confusion is not to be made. :-)
Alberto
@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.
Steven Sudit
Does it return the remaining array or the element that was removed?
agentbanks217
@agentbanks217: It returns the removed element. I updated my question to remove that ambiguity.
mipadi
It looks like `unshift` returns the new array while `shift` returns the removed value (or nil).
Steven Sudit
A: 

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.

Robert Grant
+3  A: 

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 *shift*ing all other elements, while pop and push add/remove elements at the end of the Array, so preserving other elements' indices.

Examples:

a = [2, 4, 8]
a.push(16, 32) #=> [2, 4, 8, 16, 32]
a.shift(0, 1)  #=> [0, 1, 2, 4, 8, 16, 32]
a.unshift      #=> [1, 2, 4, 8, 16, 32]
a.pop          #=> [1, 2, 4, 8, 16]
Alberto
If you were to edit your answer to summarize the the mipadi thread, I would be glad to upvote.
Steven Sudit
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.
Steven Sudit
+1  A: 

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"] 
Steve Weet
You can basically think of shift and unshift as being operations on a FIFO queue
Jaco Pretorius
A: 

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.

Tony