Paul
Paul

Reputation: 26690

How to clean STDIN in Ruby?

How to clean STDIN in Ruby? I want to be sure that nothing is left in STDIN.

Upvotes: 2

Views: 478

Answers (1)

Huluk
Huluk

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

Related Questions