[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Odd Problem with Command Line Args

Alex Ciarlillo

12/19/2006 3:27:00 PM

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):

<code>

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

</code>

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

--
Posted via http://www.ruby-....

3 Answers

Brad Phelan

12/19/2006 3:54:00 PM

0

Alex Ciarlillo wrote:
> 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):
>
> <code>
>
> 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
>
> </code>
>
> 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
>

I've seen a similar problem and I got around it by using $stdin explcitly

date = $stdin.gets


It might work??

--
http://xt...

Tom Pollard

12/19/2006 4:10:00 PM

0


On Dec 19, 2006, at 10:27 AM, Alex Ciarlillo wrote:
> 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):
>
> <code>
>
> 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
>
> </code>
>
> 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?

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


juan pedro meriño

12/19/2006 4:39:00 PM

0

how i use ruby with sesssion of this?

2006/12/19, Chunyun Zhao <chunyun.zhao@gmail.com>:
> Check out the doucmentation on gets method with: ri Kernel#gets
>
> On 12/19/06, Alex Ciarlillo <ac251404@ohio.edu> wrote:
> >
> > 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):
> >
> > <code>
> >
> > 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
> >
> > </code>
> >
> > 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
> >
> > --
> > Posted via http://www.ruby-....
> >
> >
>
>