Ruby | SizedQueue shift() function

The shift() is an inbuilt function in Ruby returns the element in the front of the SizedQueue and removes it from the SizedQueue.

Syntax: sq_name.shift()

Parameters: The function does not takes any element.

Return Value: It returns the first element which is at the front of the SizedQueue and removes it from the SizedQueue.

Example 1:




#Ruby program for shift() function in SizedQueue
  
#Create a new SizedQueue q1
sq1 = SizedQueue.new(2)
  
#push 5
          sq1.push(5)
  
#push 6
              sq1.push(6)
  
#Prints the top - most element and also shifts it
                  puts sq1.shift
  
                      puts q1.shift


Output:

5
6

Example 2:




#Ruby program for shift function in SizedQueue
  
#Create a new SizedQueue q1
sq1 = SizedQueue.new(2)
  
#push 12
          sq1.push(12)
  
#push 21
              sq1.push(21)
  
#Prints the top - most element and also shifts it
                  puts sq1.shift
  
                      puts sq1.shift


Output:

12
21

Reference: https://devdocs.io/ruby~2.5/SizedQueue#method-i-shift



Contact Us