16

I am trying to implement a simple "Press any key to continue". I print this message to the console, and I want to erase it after a key is pressed.

Following "Writing over previously output lines in the command prompt 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 doesn't work and the next puts won't erase the sentence. Is it because of a different function context? The gets spawns a newline? Or because I'm on Windows OS?

Community
  • 1
  • 1
Cyril Duchon-Doris
  • 12,964
  • 9
  • 77
  • 164
  • Btw StackOverflow syntax highlighting made me realize `continue` was a syntax keyword but apparently this is not a problem – Cyril Duchon-Doris Jan 04 '16 at 15:06
  • 2
    Ruby does not have a `continue` keyword. – Stefan Jan 04 '16 at 15:28
  • @Stefan Oh, then it was just SO's default coloring, as continue is used in several other languages... thanks for pointing it out – Cyril Duchon-Doris Jan 04 '16 at 16:02
  • 1
    Instead of overwriting the previous output, you can also use [ANSI escape codes](https://en.wikipedia.org/wiki/ANSI_escape_code), e.g. `print "press key"; STDIN.getch; print "\e[G\e[K"` (`ESC [ G` moves the cursor to the first column and `ESC [ K` clears the remaining line) – Stefan Jan 04 '16 at 16:47
  • @CyrilDD: Ruby uses `next` instead of `continue`. – Charles Jan 04 '16 at 21:31

1 Answers1

29

You can use STDIN from the IO 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 the added bonus that it only requires one character to be entered (getch - get character) allowing the 'press any key' to work without a return or enter.

Yule
  • 9,668
  • 3
  • 51
  • 72
  • 1
    It isn't necessary to add "Edit:" lines. We can see what's been changed if necessary. Simply make your change where you would have added the information in the first place. – the Tin Man Jan 04 '16 at 17:32
  • 1
    I'm downvoting this because you can't exit from the script with "ctrl+c" SIGINT or any other method if you need to interrupt during "STDIN.getch". If you use this in a loop or anything like that, there is no way to stop the script that you are running. – Dave Doga Oz Apr 28 '22 at 17:51