Skip to content

Instantly share code, notes, and snippets.

@luke-gru
Created June 18, 2011 19:44
Show Gist options
  • Save luke-gru/1033445 to your computer and use it in GitHub Desktop.
Save luke-gru/1033445 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
def quit?
begin
# See if a 'Q' has been typed yet
while c = STDIN.read_nonblock(1)
puts "I found a #{c}"
return true if c == 'Q'
end
# No 'Q' found
false
rescue Errno::EINTR
puts "Well, your device seems a little slow..."
false
rescue Errno::EAGAIN
# nothing was ready to be read
puts "Nothing to be read..."
false
rescue EOFError
# quit on the end of the input stream
# (user hit CTRL-D)
puts "Who hit CTRL-D, really?"
true
end
end
loop do
puts "I'm a loop!"
puts "Checking to see if I should quit..."
break if quit?
puts "Nope, let's take a nap"
sleep 3
puts "Onto the next iteration!"
end
puts "Oh, I quit."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment