Skip to content

Instantly share code, notes, and snippets.

@spikeheap
Last active August 7, 2021 15:36
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save spikeheap/61a729377896c6c9685e58d345ceb72e to your computer and use it in GitHub Desktop.
Save spikeheap/61a729377896c6c9685e58d345ceb72e to your computer and use it in GitHub Desktop.
Reading STDIN with threads in Ruby
require 'thread'
queue = Queue.new
producer = Thread.new do
loop do
key = STDIN.getch
queue << key if key
end
end
consumer = Thread.new do
loop do
key = queue.pop
# Pusher code here
# ctrl-c
exit if key == "\u0003"
end
end
consumer.join
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment