August 2020
Intermediate to advanced
508 pages
11h 53m
English
Queues are common in many applications, ranging from printing jobs to background workers in web applications.
Let’s say we were programming a simple Ruby interface for a printer that can accept printing jobs from various computers across a network. We want to make sure we print each document in the order in which they were received.
This code uses our Ruby implementation of the Queue class from earlier:
| | class PrintManager |
| | |
| | def initialize |
| | @queue = Queue.new |
| | end |
| | |
| | def queue_print_job(document) |
| | @queue.enqueue(document) |
| | end |
| | |
| | def run |
| | # Each time this loop runs, we read the document |
| | # at the front of the queue: |
| | while @queue.read |
| | # We dequeue the document and print it: |
| | print(@queue. ... |
Read now
Unlock full access