在`if ... end`statement中使用时,`gets`无效

时间:2013-02-08 11:34:24

标签: ruby

我想找到一种方法让我的脚本等到用户点击ENTER,同时使用if...end

input = 3

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

这不起作用,因为我

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

我应该使用什么而不是gets暂停脚本?

感谢您的时间

2 个答案:

答案 0 :(得分:3)

尝试将gets替换为$stdin.gets

答案 1 :(得分:0)

它为我工作,您确定要从控制台读取吗?

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