Ruby | SizedQueue << function
Last Updated :
09 Jan, 2020
The <<() is an inbuilt function in Ruby inserts the element in the SizedQueue. The SizedQueue have a particular capacity and cannot accept elements once it is full.
Syntax: sq_name << element
Parameters: The function takes the element to be inserted into the SizedQueue.
Return Value: It inserts the element into the SizedQueue.
Example 1:
#Ruby program for << function in SizedQueue
#Create a new SizedQUEUE q1
q1 = SizedQueue. new (2)
#push 5
q1
<< 5
#push 6
q1
<< 6
#Prints the element
puts q1.pop
puts q1.pop
|
Output:
5
6
Example 2:
#Ruby program for << function in SizedQueue
#Create a new SizedQUEUE q1
q1 = SizedQueue. new (3)
#push 15
q1
<< 15
#push 16
q1
<< 16
#push 17
q1
<< 17
#Prints the element
puts q1.pop
puts q1.pop
puts q1.pop
|
Output:
15
16
17
Reference: https://devdocs.io/ruby~2.5/sizedqueue#method-i-3C-3C
Share your thoughts in the comments
Please Login to comment...