# Odd Problem with Command Line Args

**URL:** <https://www.ruby-forum.com/t/odd-problem-with-command-line-args/79832>\
**Category:** Ruby\
**Created:** [December 19, 2006, 4:27pm UTC](https://www.ruby-forum.com/t/odd-problem-with-command-line-args/79832 "2006-12-19T16:27:23Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![combas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/aca169/32.png) [@combas](https://www.ruby-forum.com/u/combas)\
**Post date:** [December 19, 2006, 4:27pm UTC](https://www.ruby-forum.com/t/odd-problem-with-command-line-args/79832/1 "2006-12-19T16:27:23Z")

</div>

For some reason when using command line arguments to specify input  
files, whenever I use ‘gets’ it seems to be reading from the argument  
files instead of STDIN.

My code is something like this (simplified):

`
emp_file = ARGV[0]
inp_file = ARGV[1]
out_file = ARGV[2]
print "Please enter the first date of the pay period (MM/DD/YYYY): "
date = gets
puts date
exit
`

Now instead of allowing me to type a response it is just spitting out  
the first line of the file that is given by ARGV[0]. In this case the  
file is a list of 3 digit numbers and just spits out “###\n” and exits  
without allowing me to type anything. Any ideas on why this is  
happening? Do I need to clear ‘gets’ or something?  
Thanks

---

<div class="post-metadata">

**Author:** ![combas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/aca169/32.png) [@combas](https://www.ruby-forum.com/u/combas)\
**Post date:** [December 20, 2006, 7:16pm UTC](https://www.ruby-forum.com/t/odd-problem-with-command-line-args/79832/2 "2006-12-20T19:16:34Z")

</div>

Check out the doucmentation on gets method with: ri Kernel#gets

---

<div class="post-metadata">

**Author:** ![combas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/aca169/32.png) [@combas](https://www.ruby-forum.com/u/combas)\
**Post date:** [December 20, 2006, 7:20pm UTC](https://www.ruby-forum.com/t/odd-problem-with-command-line-args/79832/3 "2006-12-20T19:20:44Z")

</div>

On Dec 19, 2006, at 10:27 AM, Alex C. wrote:

> out\_file = ARGV[2]  
> file is a list of 3 digit numbers and just spits out “###\n” and exits  
> without allowing me to type anything. Any ideas on why this is  
> happening?

It sounds like Ruby reproduces Perl’s magical treatment of the  
commandline args. (Input is taken from the files listed on the  
command line, unles stdin is redirected.)

Perhaps you need to clear the ARGV array after reading the values  
out? For instance, use

emp\_file = ARGV.shift  
inp\_file = ARGV.shift  
out\_file = ARGV.shift

instead of what you had above.

TomP
