### Ruby: Demonstrating Event Coalescing with IO::Watch Source: https://context7.com/socketry/io-watch/llms.txt This snippet demonstrates how IO::Watch coalesces multiple rapid writes to the same file into a single filesystem event. It sets up a monitor, triggers several writes, and verifies that only one event is received. Dependencies include the 'io/watch' gem and standard Ruby libraries for temporary directories and threads. ```ruby require 'io/watch' require 'tmpdir' Dir.mktmpdir do |root| changes = Thread::Queue.new thread = Thread.new do monitor = IO::Watch::Monitor.new([root]) monitor.run do |event| changes << event end end changes.pop # Wait for start test_file = File.join(root, 'test.txt') # Multiple rapid writes File.write(test_file, 'first') File.write(test_file, 'second') File.write(test_file, 'third') # Only one event is triggered event = changes.pop puts "Single coalesced event: #{event[:root]}" # Queue remains empty despite multiple writes sleep 0.5 if changes.empty? puts "No additional events - changes were coalesced" end thread.kill end ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.