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?
5 Answers
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"]
-
9You can basically think of shift and unshift as being operations on a FIFO queue Sep 16, 2010 at 9:13
-
@JacoPretorius Huh?
shift
andunshift
act like a FILO stack,push
andshift
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
-
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]
-
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.– mckenzmJul 27, 2016 at 23:08
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.
-
8Well 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
-
@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
-
5I was just pointing out that, since
pop
andpush
are alsoArray
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
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.
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.
shift/unshift
are likepush/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. :)push
it into the top, andshift
it out the other end.