How to encode "press the key to continue"

I am trying to implement a simple "Press any key to continue." I print this message to the console and I want to delete it after pressing a key.

After " Writing Previously Displayed Strings on the Command Line with ruby" , I tried this piece of code:

def continue print "Press any key to continue\r" gets end puts "An awesome story begins..." continue puts "And ends after 2 lines" 

However, the \r trick does not work, and the next puts will not erase the sentence. Is it due to a different context of the function? gets spawns a new line? Or because I'm on Windows?

+5
source share
1 answer

You can use STDIN from the I / O class rather than gets .

 require 'io/console' def continue_story print "press any key" STDIN.getch print " \r" # extra space to overwrite in case next sentence is short end puts "An awesome story begins..." continue_story puts "And ends after 2 lines" 

This has an additional bonus, which requires only one character to be entered ( getch - get a character), allowing you to "press any key" to work without returning or entering.

+10
source

Source: https://habr.com/ru/post/1239797/


All Articles