Is there a clash between gets and ARGV?

Y

ycsunil

Hello all,

Here is my small ruby program:

if (ARGV[0] == 'add')
puts 'input something'
gt = gets
puts gt
else
puts 'fail'
end

If I try to execute above program am getting below error:

C:\Documents and Settings\sunilc\Desktop>ruby test.rb 'add'
input something
test.rb:3:in `gets': No such file or directory - add (Errno::ENOENT)
from test.rb:3

Please let me know why is this error coming?

-Sunil
 
S

Sebastian Hungerecker

ycsunil said:
C:\Documents and Settings\sunilc\Desktop>ruby test.rb 'add'
input something
test.rb:3:in `gets': No such file or directory - add (Errno::ENOENT)
=A0 =A0 =A0 =A0 from test.rb:3

Please let me know why is this error coming?

Kernel#gets tries to read from the file specified by the command line=20
arguments if there are any. This is documented (and in some cases useful).
If you don't want this behaviour use IO#gets directly (as in: STDIN.gets).

HTH,
Sebastian
=2D-=20
Jabber: (e-mail address removed)
ICQ: 205544826
 
R

Robert Klemme

2008/2/20 said:
Kernel#gets tries to read from the file specified by the command line
arguments if there are any. This is documented (and in some cases useful).
If you don't want this behaviour use IO#gets directly (as in: STDIN.gets).

Alternatively the script can be fixed like this:

if (ARGV.shift == 'add')
puts 'input something'
gt = gets
puts gt
else
puts 'fail'
end

A more complex but also more robust alternative might be to process
command line options e.g. with OptionParser.

Kind regards

robert
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top