2013-02-08 39 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