LANGUAGES Signal 124
Using a Ruby Fiber to convert block-scoped APIs into start and stop methods
Illustration only Photo by Mika Baumeister on Unsplash
A developer demonstrates using a Ruby Fiber to invert a block-scoped library API into distinct start and stop methods, avoiding the overhead of threads and queues.
This pattern solves lifecycle mismatches where a framework like RSpec provides setup and teardown hooks but no around hook. It offers a lightweight, blocking alternative to threads for smuggling resources out of blocks.
Written by elseif from the cluster below · every claim links back to a sourceThe three things worth knowing
A Fiber can convert a block-scoped API into start and stop methods by yielding the resource on the first resume and finishing the block on the second.
This avoids the overhead and complexity of using a Thread with two Queues to manage execution flow.
The pattern addresses framework mismatches, such as RSpec lacking an around(:suite) hook for block-scoped libraries.
THE CLUSTER