Forum: Ruby gets problem - strange

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
8ecb8bb62c3283f8069a54056c7dc25f?d=identicon&s=25 Jim Freeze (Guest)
on 2006-06-11 20:23
(Received via mailing list)
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 Freeze
Cee0292fffa691f1fb320d5400200e99?d=identicon&s=25 Marcel Molina Jr. (Guest)
on 2006-06-11 20:39
(Received via mailing list)
On Mon, Jun 12, 2006 at 03:21:18AM +0900, Jim Freeze 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
A6ce942e03edad55d9b504c1e1d859d6?d=identicon&s=25 Jim Freeze (Guest)
on 2006-06-11 20:46
(Received via mailing list)
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 Freeze
31af45939fec7e3c4ed8a798c0bd9b1a?d=identicon&s=25 Matthew Smillie (notmatt)
on 2006-06-11 20:49
(Received via mailing list)
On Jun 11, 2006, at 19:21, Jim Freeze 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

"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.
C1bcb559f87f356698cfad9f6d630235?d=identicon&s=25 Hal Fulton (Guest)
on 2006-06-11 20:49
(Received via mailing list)
Jim Freeze 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
Cee0292fffa691f1fb320d5400200e99?d=identicon&s=25 Marcel Molina Jr. (Guest)
on 2006-06-11 20:49
(Received via mailing list)
On Mon, Jun 12, 2006 at 03:44:53AM +0900, Jim Freeze 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
This topic is locked and can not be replied to.