What Does Ruby’s Array#shift do?

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"] 

Leave a Comment

File not found.