ARGF: No switch to STDIN after all files?

I've started learning Ruby and trying to do the first (stupid) program
as exercise.
The input will be supported from a file and/or stdin.
If no file given, the program prompt user to insert all the data.
If a file is given, it expects line with at least two integers and will
read until the first bad line. After that, we expect another valid line.
If while reading the first sequence the file(s) end, a feature require
the user to insert some other data.
And there's the problem!

There's a sample program:

while line = gets
   n1, n2 = line.scan(/\d+/).map(&:to_i)
   break unless n2
   # ... processing ...
end

# At first try, the condition was ARGF.eof? but it will raise IOError.
# How and when I've to use eof? method?
if ARGF.closed?
  # tell user what to do
end

n1, n2 = gets.scan(/\d+/).map(&:to_i) # Raise: NoMethodError on Nil
class

Also if ARGV is empty, ARGF doesn't swap to STDIN and gets continue to
return nil.
I've tried ARGV.rewind, but got an ArgumentError "no stream to rewind".

So, is there a method to swap to STDIN after reading all files? Thanks
in advance :slight_smile:

Some tries:
ARGV << 'somefile'
ARGF.readlines # || .close || .skip
ARGV # => [] ok!
gets # => nil damn...
ARGF.filename # => 'somefile' No swap!
ARGF.closed? # => true
ARGF.skip.filename # => 'somefile' Again...

PS: I've using Ruby1.9.3 on Linux

···

--
Posted via http://www.ruby-forum.com/.

I've started learning Ruby and trying to do the first (stupid) program
as exercise.
The input will be supported from a file and/or stdin.
If no file given, the program prompt user to insert all the data.
If a file is given, it expects line with at least two integers and will
read until the first bad line. After that, we expect another valid line.
If while reading the first sequence the file(s) end, a feature require
the user to insert some other data.
And there's the problem!

Also if ARGV is empty, ARGF doesn't swap to STDIN and gets continue to
return nil.

ARGF switches to $stdin only if ARGV is empty *initially*, i.e. when
the first read attempt is made.

I've tried ARGV.rewind, but got an ArgumentError "no stream to rewind".

So, is there a method to swap to STDIN after reading all files? Thanks
in advance :slight_smile:

You can push "-" onto ARGV which will make ARGF switch to STDIN - this
even works when you have read from ARGF already:

~$ seq 20 25 | ruby19 -e '2.times { ARGF.each {|l| printf "%s:
%s",ARGF.filename,l}; ARGV << "-" }' <(seq 10 15)
/dev/fd/63: 10
/dev/fd/63: 11
/dev/fd/63: 12
/dev/fd/63: 13
/dev/fd/63: 14
/dev/fd/63: 15
-: 20
-: 21
-: 22
-: 23
-: 24
-: 25

Kind regards

robert

···

On Wed, Jul 4, 2012 at 4:03 AM, Iazel Datenshi <lists@ruby-forum.com> wrote:

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

Robert Klemme wrote in post #1067315:

You can push "-" onto ARGV which will make ARGF switch to STDIN - this
even works when you have read from ARGF already:

Kind regards

robert

That's exactly what I want, thanks a lot! :slight_smile:

···

--
Posted via http://www.ruby-forum.com/\.