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

293 views Asked by At

So, I just learned about ARGV and the arguments, and I'm trying to combine gets.to_i (or gets.chomp) in the same script. But it ain't happening. Any suggestions?

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

I keep getting the following error message:

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>'

The output I get, when not adding the gets.to_i, is: "you will first go to point alpha, then bravo, then foxtrot, finishing off with echo and finally delta."

1

There are 1 answers

0
vveleva On BEST ANSWER

ARGV looks like an array, but it doesn't behave exactly like one. You won't have access to the second argument, unless you remove the first one first. Your code works if you rewrite it like this:

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