2015-06-14 34 views
1

因此,我剛剛瞭解了ARGV和參數,並且試圖將gets.to_i(或gets.chomp)結合到同一個腳本中。但它沒有發生。有什麼建議麼?我無法將gets.to_i與ARGV參數結合起來,在Ruby中

a, b, c, d, e, f = ARGV 

puts "you will first go to point #{a}, then #{b}, then #{f}, finishing off with #{e} and finally #{d}." 

print "Give me a number: " 
time = gets.to_i 

puts "you can start at #{time}" 

我不斷收到以下錯誤信息:

Give me a number: ex13.rb:12:in `gets': No such file or directory @ rb_sysopen - alpha (Errno::ENOENT) 
    from ex13.rb:12:in `gets' 
    from ex13.rb:12:in `<main>' 

輸出我得到的,在不添加gets.to_i是: 「你先去點α,然後喝彩,然後是狐步舞,用回聲完成,最後是三角洲。「

+0

腳本不產生任何錯誤 –

+1

見http://stackoverflow.com/q/2166862 – cremno

+0

看起來不像同樣的問題:如果你重寫它這樣你的代碼工作。如果是這樣,它不能幫助我解決問題。令人懷疑的是,我可以解釋我的問題與假設的重複問題有何不同。 – Padawan

回答

1

ARGV看起來像一個數組,但它不像一個完全一樣。除非您先刪除第一個參數,否則您將無法訪問第二個參數。

a, b, c, d, e, f = (0..5).map { ARGV.shift } 

puts "you will first go to point #{a}, then #{b}, then #{f}, finishing off with #{e} and finally #{d}." 

print "Give me a number: " 
time = gets.to_i 

puts "you can start at #{time}" 
相關問題