Ruby | SizedQueue length() function
Last Updated :
09 Jan, 2020
The length() is an inbuilt function in Ruby returns the current length of the SizedQueue or the number of objects present in it. It does not returns the pre-defined size of the SizedQueue
Syntax: sq_name.length()
Parameters: The function does not takes any parameter.
Return Value: It returns the number of elements in the SizedQueue.
Example 1:
#Ruby program for length() function in SizedQueue
#Create a new SizedQueue q1
sq1 = SizedQueue. new (2)
#pushes 5
sq1.enq(5)
#pushes 6
sq1.enq(6)
#Prints the length
puts sq1.length
#Pops the element
sq1.pop
#Prints the length
puts sq1.length
|
Output:
2
1
Example 2:
#Ruby program for length() function in SizedQueue
#Create a new SizedQueue q1
sq1 = SizedQueue. new (3)
#Prints the length
puts sq1.length
#pushes 5
sq1.enq(5)
#pushes 6
sq1.enq(6)
#pushes 7
sq1.enq(7)
#Prints the length
puts sq1.length
#Pops the element
sq1.pop
#Prints the length
puts sq1.length
|
Output:
0
3
2
Reference: https://devdocs.io/ruby~2.5/sizedqueue#method-i-length
Share your thoughts in the comments
Please Login to comment...