What is the queue.empty? function in Ruby?

The queue.empty? function in Ruby returns a true value if the queue is empty; otherwise, it returns false. In short, this function is used to check if the queue is empty or not.

Figure 1 shows the visual representation of the queue.empty? function.

Note: Queue is a data structure in Ruby that acts as a waiting list and uses the FIFOfirst-in-first-out approach.

Figure 1: Visual representation of queue.empty? function

Syntax

queue_name.empty?
# where the queue_name is the name of the queue

Parameter

This function does not require a parameter.

Return value

If the queue is empty, the queue.empty? function returns a true value. Otherwise, it returns false.

Code

# two queues
# filled queue
queue1 = Queue.new
queue1.push(1)
queue1.push(3)
queue1.push(5)
queue1.push(2)
queue1.push(4)
print "The queue1 is empty : ", queue1.empty?, "\n"
# empty queue
queue2 = Queue.new
print "The queue2 is empty : ", queue2.empty?, "\n"

Free Resources