# Strange ruby problem

**URL:** <https://www.ruby-forum.com/t/strange-ruby-problem/149411>\
**Category:** Ruby\
**Created:** [September 24, 2008, 4:21pm UTC](https://www.ruby-forum.com/t/strange-ruby-problem/149411 "2008-09-24T16:21:11Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [September 24, 2008, 4:21pm UTC](https://www.ruby-forum.com/t/strange-ruby-problem/149411/1 "2008-09-24T16:21:11Z")

</div>

So i’ve cut the program down to about 4 lines to give the idea of  
whats going on:

num = ARGV[0]  
num2 = ARGV[1]  
puts " #{num} #{num2} \n"  
line = gets

if i give the program some input from the command line it fails and  
sais " gets: No such file or directory -1"  
yet if i supply no input the program goes straight to gets. This seems  
fairly strange to me. Though when I did this:

num = ARGV[0]  
num2 = ARGV[1]  
puts " #{num} #{num2} \n"  
STDOUT.flush  
line = STDIN.gets

everything works normal. Why is this and whats going on? Thanks in  
advance

Sincerely,  
Chris Dancy

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [September 24, 2008, 4:30pm UTC](https://www.ruby-forum.com/t/strange-ruby-problem/149411/2 "2008-09-24T16:30:35Z")

</div>

try without the last line, line = gets

On Wed, Sep 24, 2008 at 7:49 PM, removed\_email\_address@domain.invalid \<

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [September 24, 2008, 4:43pm UTC](https://www.ruby-forum.com/t/strange-ruby-problem/149411/3 "2008-09-24T16:43:31Z")

</div>

The program is trying to read from ARGV[0] , and there is no file with  
the  
name ARGV[0] that you have given.

On Wed, Sep 24, 2008 at 7:49 PM, removed\_email\_address@domain.invalid \<

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [September 24, 2008, 6:35pm UTC](https://www.ruby-forum.com/t/strange-ruby-problem/149411/4 "2008-09-24T18:35:45Z")

</div>

On Sep 24, 10:18 am, “removed\_email\_address@domain.invalid”  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> yet if i supply no input the program goes straight to gets. This seems  
> fairly strange to me. Though when I did this:
> 
> num = ARGV[0]  
> num2 = ARGV[1]  
> puts " #{num} #{num2} \n"  
> STDOUT.flush  
> line = STDIN.gets
> 
> everything works normal. Why is this and whats going on? Thanks in  
> advance

I think we need to see your command-line arguments and whether you’re  
putting them before the name of the Ruby script or after. Are you  
invoking the script like:

```
ruby script.rb arg1 arg2

```

or:

```
ruby arg1 arg2 script.rb

```

because the first is what you should be doing. Alternatively, do you  
have RUBYOPT set or do you invoke any arguments on your #! line (if  
you’re on a Unix-like system).

Eric

====

[LearnRuby.com](http://LearnRuby.com) offers Rails & Ruby hands-on public & on-site workshops.  
Ruby Fundamentals Workshop Oct 8-10 Ann Arbor, Mich.  
(Note: Great Lakes Ruby Bash in Ann Arbor on Oct. 11; attend both!)  
Please visit [http://LearnRuby.com](http://LearnRuby.com) for all the details.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [September 24, 2008, 8:31pm UTC](https://www.ruby-forum.com/t/strange-ruby-problem/149411/5 "2008-09-24T20:31:46Z")

</div>

On 24.09.2008 16:18, removed\_email\_address@domain.invalid wrote:

> sais " gets: No such file or directory -1"  
> advance  
> As others have said: arguments are the problem. “gets” tries to read  
> from ARGF which is basically all ARGV values interpreted as files and  
> concatenated as streams. You typically use this for poor man’s cat:

ruby -e ‘ARGF.each {|line| puts line}’ foo bar baz

You can avoid the issue by doing

num = ARGV.shift  
num2 = ARGV.shift  
puts num, num2  
line = gets

Cheers

robert

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [September 24, 2008, 11:58pm UTC](https://www.ruby-forum.com/t/strange-ruby-problem/149411/6 "2008-09-24T23:58:57Z")

</div>

On Wed, Sep 24, 2008 at 1:29 PM, Robert K.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> num2 = ARGV.shift  
> puts num, num2  
> line = gets
> 
> Cheers
> 
> ```
> robert
> 
> ```

As robert says.

I ran into this just the other day. ARGV has to be clear (empty), for  
stdin to work for gets. (Pickaxe 2nd Ed., p. 180)

Todd

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [September 25, 2008, 1:55am UTC](https://www.ruby-forum.com/t/strange-ruby-problem/149411/7 "2008-09-25T01:55:33Z")

</div>

On Sep 24, 9:18 am, “removed\_email\_address@domain.invalid”  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> yet if i supply no input the program goes straight to gets. This seems
> 
> Sincerely,  
> Chris Dancy

Arguments on the command line are assumed  
to be files to be read by “gets”.

E:\>echo First line of file \> temp.txt

E:\>ruby -e “p gets” temp.txt  
“First line of file \n”

Correct programs:

num = ARGV.shift  
num2 = ARGV.shift  
puts " #{num} #{num2}"  
line = gets

num = ARGV[0]  
num2 = ARGV[1]  
puts " #{num} #{num2}"  
line = $stdin.gets
