r/ruby • u/keyslemur • Jun 08 '26
Beyond Enumerable: Heaps and Priority Queues
https://baweaver.com/writing/2026/06/06/beyond-enumerable-heaps-and-priority-queues/Honestly I never really understood how these were written and what the reasoning behind them was as much as I just reached for existing gems or frameworks, so as I tend to do I learn by teaching and writing.
17
Upvotes
7
u/pikrua Jun 08 '26
I realized this when I was studying leetcode. With ruby I have no chance of clearing any timed technical interview problem that requires a heap.
Disclaimer before the replies: I know that kind of interview is pointless, everyone knows.
5
u/mperham Sidekiq Jun 08 '26
Yep, a Heap is the only structure Iâve found lacking in stdlib. I vendor a copy of https://github.com/kanwei/algorithms/blob/master/lib/containers/heap.rb for use in Sidekiq Enterprise. Itâs the structure that keeps cron jobs sorted by time of next execution.
5
u/schneems Puma maintainer Jun 08 '26
A priority queue already exists and has shipped with Ruby since 3.2 but (big but) itâs an unstable/internal interface since it ships as an implementation detail of syntax suggest (I maintain)Â https://github.com/ruby/syntax_suggest/blob/main/lib/syntax_suggest/priority_queue.rb.
2
u/CaptainKabob Jun 09 '26
Great write up! As all us library maintainers share implementations, in Concurrent Ruby we have a ânon concurrent priority queueâ: https://github.com/ruby-concurrency/concurrent-ruby/blob/master/lib/concurrent-ruby/concurrent/collection/non_concurrent_priority_queue.rb
1
u/[deleted] Jun 09 '26
[removed] â view removed comment