Reputation: 26690
How to clean STDIN in Ruby? I want to be sure that nothing is left in STDIN.
Upvotes: 2
Views: 478
Reputation: 874
You can check if there is input available in STDIN using IO#ready?
from io/wait
.
Use IO#getc
to read a character, removing it from the buffer.
require 'io/wait'
def clear_stdin
$stdin.getc while $stdin.ready?
end
Upvotes: 1