# Gets problem - strange

**URL:** <https://www.ruby-forum.com/t/gets-problem-strange/63085>\
**Category:** Ruby\
**Created:** [June 11, 2006, 8:23pm UTC](https://www.ruby-forum.com/t/gets-problem-strange/63085 "2006-06-11T20:23:53Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![sauy7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ecc23a/32.png) [@sauy7](https://www.ruby-forum.com/u/sauy7)\
**Post date:** [June 11, 2006, 8:23pm UTC](https://www.ruby-forum.com/t/gets-problem-strange/63085/1 "2006-06-11T20:23:53Z")

</div>

This is a problem that was pointed out by Esteban Velazquez.

% cat gets2.rb  
#!/usr/bin/ruby

print "getting "; puts gets

Sending the app arguments returns an error:

% ruby gets2.rb fred  
gets2.rb:3:in `gets’: No such file or directory - fred  
(Errno::ENOENT)  
from gets2.rb:3

But, with no arguments, it runs ok.

% ruby gets2.rb  
getting foo  
foo

The problem is eluding me now. Any help would be appreciated.

Jim F.

---

<div class="post-metadata">

**Author:** ![sauy7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ecc23a/32.png) [@sauy7](https://www.ruby-forum.com/u/sauy7)\
**Post date:** [June 11, 2006, 8:39pm UTC](https://www.ruby-forum.com/t/gets-problem-strange/63085/2 "2006-06-11T20:39:45Z")

</div>

On Mon, Jun 12, 2006 at 03:21:18AM +0900, Jim F. wrote:

> gets2.rb:3:in `gets’: No such file or directory - fred  
> (Errno::ENOENT)  
> from gets2.rb:3
> 
> But, with no arguments, it runs ok.
> 
> % ruby gets2.rb  
> getting foo  
> foo
> 
> The problem is eluding me now. Any help would be appreciated.

It works like perl in this regard, expecting the arguments on the  
command  
line to be files the lines of which you want read.

% echo -e “foo\nbar” \> some\_file  
% echo “while l = gets; p l end” \> gets.rb  
% ruby gets.rb some\_file  
“foo\n”  
“bar\n”

marcel

---

<div class="post-metadata">

**Author:** ![sauy7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ecc23a/32.png) [@sauy7](https://www.ruby-forum.com/u/sauy7)\
**Post date:** [June 11, 2006, 8:46pm UTC](https://www.ruby-forum.com/t/gets-problem-strange/63085/3 "2006-06-11T20:46:09Z")

</div>

On Jun 11, 2006, at 1:36 PM, Marcel Molina Jr. wrote:

> It works like perl in this regard, expecting the arguments on the  
> command  
> line to be files the lines of which you want read.
> 
> % echo -e “foo\nbar” \> some\_file  
> % echo “while l = gets; p l end” \> gets.rb  
> % ruby gets.rb some\_file  
> “foo\n”  
> “bar\n”

So, I suppose that one needs to empty out ARGV before using gets?

Wow. Really?!

Jim F.

---

<div class="post-metadata">

**Author:** ![sauy7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ecc23a/32.png) [@sauy7](https://www.ruby-forum.com/u/sauy7)\
**Post date:** [June 11, 2006, 8:49pm UTC](https://www.ruby-forum.com/t/gets-problem-strange/63085/4 "2006-06-11T20:49:44Z")

</div>

Jim F. wrote:

> > “bar\n”
> 
> So, I suppose that one needs to empty out ARGV before using gets?
> 
> Wow. Really?!

ARGF really. This bites me sometimes. I have difficulty  
remembering the details of how it works.

Hal

---

<div class="post-metadata">

**Author:** ![sauy7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ecc23a/32.png) [@sauy7](https://www.ruby-forum.com/u/sauy7)\
**Post date:** [June 11, 2006, 8:49pm UTC](https://www.ruby-forum.com/t/gets-problem-strange/63085/5 "2006-06-11T20:49:31Z")

</div>

On Jun 11, 2006, at 19:21, Jim F. wrote:

> gets2.rb:3:in `gets’: No such file or directory - fred  
> (Errno::ENOENT)  
> from gets2.rb:3
> 
> But, with no arguments, it runs ok.

Check the documentation for the method:  
[http://ruby-doc.org/core/classes/Kernel.html#M002976](http://ruby-doc.org/core/classes/Kernel.html#M002976)

“Returns (and assigns to $\_) the next line from the list of files in  
ARGV (or $\*), or from standard input if no files are present on the  
command line.”

You can explicitly use $stdin.gets or STDIN.gets to get the behaviour  
you want.

matthew smillie.

---

<div class="post-metadata">

**Author:** ![sauy7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ecc23a/32.png) [@sauy7](https://www.ruby-forum.com/u/sauy7)\
**Post date:** [June 11, 2006, 8:49pm UTC](https://www.ruby-forum.com/t/gets-problem-strange/63085/6 "2006-06-11T20:49:58Z")

</div>

On Mon, Jun 12, 2006 at 03:44:53AM +0900, Jim F. wrote:

> > “bar\n”
> 
> So, I suppose that one needs to empty out ARGV before using gets?
> 
> Wow. Really?!

You want IO#gets not Kernel#gets.

STDIN.gets

marcel
