2015-06-14 70 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}" 
相关问题