Stephen Lewis
5/15/2005 2:50:00 AM
On Sun, May 15, 2005 at 06:20:24AM +0900, David Vincelli wrote:
> The program works fine except when I hit CTRL-D. After I hit CTRL-D, the
> program runs in an endless loop and seems to skip over the gets statement.
> Doesn't look good, I can't even break out of it (CTRL-C). Of course CTRL-D
> indicates end of file. My guess is that this closes STDIN.
<snip>
> I think it might be better to simply re-open STDIN as if nothing had
> happened.. is this a good idea and is it possible?
I don't think it actually closes stdin - try this:
while true
puts "type something"
$stdin.gets
if $stdin.closed?
puts "it's closed"
break
elsif $stdin.eof?
puts "apparently eof"
$stdin.seek( 0, IO::SEEK_CUR )
end
end
Ruby doesn't seem to have an equivalent to clearerr(3), which might
help here - but a successful seek is supposed to reset the eof flag,
though it's fairly ugly, and isn't guaranteed to succeed (it'll
probably break with pipes for one thing). You might try an
ungetc/getc pair as another workaround, but there's probably a much
nicer way.
--
Stephen Lewis