Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

ruby
queue
communitycreator

What is the queue.empty? function 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.

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"
Implementation of queue.empty? in Ruby

RELATED TAGS

ruby
queue
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.

Keep Exploring