AshClarke
AshClarke

Reputation: 3078

ruby gets 'enter key'

How can I recognise input of the Enter key? The following code won't work for me

puts 'press the enter key'
names = gets

if names == '\n'
    puts 'yay'
end

Upvotes: 2

Views: 14501

Answers (1)

BlakeWilliams
BlakeWilliams

Reputation: 940

This doesn't work because '\n' looks for the characters, \n instead of a new line. You want to use "\n" so it will actually read it as a newline instead of actual characters.

if names == "\n"
    puts "yay"
end

Upvotes: 6

Related Questions