# Is there a clash between gets and ARGV?

**URL:** <https://www.ruby-forum.com/t/is-there-a-clash-between-gets-and-argv/129640>\
**Category:** Ruby\
**Created:** [February 20, 2008, 10:05am UTC](https://www.ruby-forum.com/t/is-there-a-clash-between-gets-and-argv/129640 "2008-02-20T10:05:35Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ycsunil](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/6f9a4e/32.png) [@ycsunil](https://www.ruby-forum.com/u/ycsunil)\
**Post date:** [February 20, 2008, 10:05am UTC](https://www.ruby-forum.com/t/is-there-a-clash-between-gets-and-argv/129640/1 "2008-02-20T10:05:35Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ycsunil](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/6f9a4e/32.png) [@ycsunil](https://www.ruby-forum.com/u/ycsunil)\
**Post date:** [February 20, 2008, 10:16am UTC](https://www.ruby-forum.com/t/is-there-a-clash-between-gets-and-argv/129640/2 "2008-02-20T10:16:57Z")

</div>

ycsunil wrote:

> 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?

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

HTH,  
Sebastian

---

<div class="post-metadata">

**Author:** ![ycsunil](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/6f9a4e/32.png) [@ycsunil](https://www.ruby-forum.com/u/ycsunil)\
**Post date:** [February 20, 2008, 2:16pm UTC](https://www.ruby-forum.com/t/is-there-a-clash-between-gets-and-argv/129640/3 "2008-02-20T14:16:02Z")

</div>

2008/2/20, Sebastian H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> 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
