3

I've narrowed the problem down to the following simple code snippet:

#!/usr/bin/env ruby

print "Enter your name: "
name = gets.chomp
puts "Hello #{name}"

This works fine when called from the OS X Terminal like this ruby a.rb. However passing a command line argument like this ruby a.rb 123 causes this error:

a.rb:4:in `gets': No such file or directory - 123 (Errno::ENOENT)
    from a.rb:4

My goal is to pass command line arguments to a script and read input from the keyboard.

What is causing the above error?

2

1 Answer 1

2

Found the answer here: How can same program in ruby accept input from user as well as command line arguments

Just had to do ARGV.clear before using gets.

Sign up to request clarification or add additional context in comments.

1 Comment

This was helpful. It seems to be a problem for me in ruby 2.0.0-p247 but not in other rubies or patch versions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.