Select in Ruby
Our example demonstrates how to wait on multiple channel operations using Ruby’s Thread
and Queue
classes. This combination provides a powerful feature for concurrent programming in Ruby.
We receive the values "one"
and then "two"
as expected.
Note that the total execution time is only ~2 seconds since both the 1 and 2 second sleep
operations execute concurrently.
In this Ruby implementation, we use Thread
to create concurrent operations and Queue
to simulate channels. The select
functionality is mimicked by creating separate threads for each queue and using another queue (result
) to collect the first available message. This approach allows us to wait on multiple operations simultaneously, similar to Go’s select
.