Guillaume
Guillaume

Reputation: 27

`gets`not working when used in `if...end`statement

I want to find a way for my script to wait until the user hits ENTER, while using a if...end

input = 3

if input > 2    
    puts "input is greater than 2"
    gets
    puts "this shouldn't appear before I type ENTER"
end

This does not work as I get

$input is greater than 2
$this shouldn't appear before I type ENTER

what should I use instead of gets to pause the script ?

Thank you for your time

Upvotes: 0

Views: 356

Answers (2)

Byscripts
Byscripts

Reputation: 2588

Try to replace gets with $stdin.gets

Upvotes: 3

Salil
Salil

Reputation: 47532

Its working for me, are your sure you want to read from console?

input = 3

if input > 2    
    puts "input is greater than 2"
    puts "please enter your name"
    name = gets
    puts "hi #{name} this shouldn't appear before I type ENTER"
end

o/p

~/Desktop$ ruby demo.rb
input is greater than 2
please enter your name
salil
hi salil
 this shouldn't appear before I type ENTER

Upvotes: 0

Related Questions