在 Ruby 中无法将 gets.to_i 与 ARGV 参数组合

Am unable to combine gets.to_i with ARGV arguments, in Ruby

所以,我刚刚了解了 ARGV 和参数,我正在尝试在同一个脚本中组合 gets.to_i(或 gets.chomp)。但它没有发生。有什么建议么?

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 时,我得到的输出是: "you will first go to point alpha, then bravo, then foxtrot, finishing off with echo and finally delta."

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}"