Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

ruby
queue
length
communitycreator

What is queue.length() in Ruby?

Harris Amjad

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Answers Code

The queue.length() function in Ruby returns the number of elements in the queue. In short, this function is used to get the current size of the queue.

Figure 1 shows the visual representation of the queue.length() function.

Figure 1: Visual representation of queue.length() function

Syntax

queue_name.length()
# where the queue_name is the name of the queue

Parameter

This function does not require a parameter.

Return value

This function returns the number of elements in the queue, because it is used to get the current size of the queue.

Example

#filled queue
queue1=Queue.new
queue1.enq(1)
queue1.enq(3)
queue1.enq(5)
queue1.enq(2)
queue1.enq(0)
#queue1 = 1->3->5->2->0
print "The queue1.length() :",queue1.length()," \n"
#empty queue
queue2=Queue.new
print "The queue2.length() :",queue2.length()," \n"

RELATED TAGS

ruby
queue
length
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Answers Code
Keep Exploring