2013-02-08 38 views
0

我想找到我的剧本要等到用户点击回车键时使用if...end`gets`not工作,如果... end`statement

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暂停脚本?

谢谢你的时间

+0

,您在控制台看什么?或从一个文件? – 2013-02-08 11:36:16

+0

代码对我来说暂停。是否有更多的代码? – 2013-02-08 11:47:26

回答

3

尝试更换gets$stdin.gets

+0

这是为什么需要? – codener 2016-06-21 09:08:04

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