0

I have the following code:

require 'io/console'
loop do
  character = STDIN.getch
  break if character == "\n"
  print "*"
end

The code is working, but I have to press the Enter key then another key to break the loop. Why? I have this issue only with the line feed.

Sisyphe
  • 146
  • 1
  • 13
  • For me it loops forever. If I add a `puts "<#{character.inspect}>"` after `character = STDIN.getch`, it displays `*<"\r">`. So pressing Enter (on my Mac) generates a Carriage Return, not a NewLine. The loop exits if `break if character == "\r"`. – BernardK Nov 11 '17 at 00:23
  • On Windows, it generates a Line feed, not a Carriage return. I forgot it was platform dependent. – Sisyphe Nov 11 '17 at 09:25

0 Answers0