MrDatabase
MrDatabase

Reputation: 44505

Trouble passing command line arguments to Ruby script and reading from command line

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?

Upvotes: 3

Views: 1282

Answers (1)

MrDatabase
MrDatabase

Reputation: 44505

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.

Upvotes: 2

Related Questions