From: Robert Klemme Date: 2012-07-04T15:44:18+09:00 Subject: Re: ARGF: No switch to STDIN after all files? On Wed, Jul 4, 2012 at 4:03 AM, Iazel Datenshi wrote: > 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 :) 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 -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/