On Tue, 28 Mar 2006, Eric Armstrong wrote:

> ara.t.howard / noaa.gov wrote:
>> gets is a method of IO and it does read from piped input.
>> however, there is a magic variable called ARGF which is the list of
>> all files on the command line, or stdin if none are given.  so 'cat',
>> in ruby could be written as
>>
>>   ARGF.each{|line| print line}
>>
> So far, so good. Makes perfect sense.
> What is more perplexing is that the formula for console I/O
> would look like this:
>
>   puts prompt_string
>   STDIN.gets               --succeeds?
>
> Why would standard input work there, but fail in this case:
>
>   puts prompt_string
>   gets                     --fails?
>
> If no file is specified on the command line, why doesn't that
> invocation gets do the same thing as STDIN.gets?

ruby doesn't know that you didn't mean to say to read the file '60', which is
on the command line.

when you use ARGF, deliberately or not, you telling ruby that ANY command line
argument can be opened are read from.  in your case you gave '60' and this
file does not exist.  your code will work if you parse the command line first:

     harp:~ > cat a.rb
     print ARGF.read

     harp:~ > echo 42|ruby a.rb 60
     a.rb:1:in `read': No such file or directory - 60 (Errno::ENOENT)
             from a.rb:1


     harp:~ > cat a.rb
     ARGV.shift
     print ARGF.read

     harp:~ > echo 42|ruby a.rb 60
     42


regards.

-a
-- 
share your knowledge.  it's a way to achieve immortality.
- h.h. the 14th dali lama