Bug #1633: ARGF#close and ARGF#skip are Often Fatal on 1.9
http://redmine.ruby-lang.org/issues/show/1633

Author: Run Paint Run Run
Status: Open, Priority: Normal
Category: core
ruby -v: ruby 1.9.2dev (2009-06-14 trunk 23689) [i686-linux]

I don't claim to fully understand this issue, but I'd like to explain some differences between ARGF#close/#skip on 1.8 and 1.9 which seem peculiar.

If ARGV is empty, ARGF refers to STDIN. On all versions ARGF#close suceeds, but ARGF#closed? returns false on 1.9; true on earlier versions.

    $ ruby86 -ve 'ARGF.close; p ARGF.closed?'
    ruby 1.8.6 (2009-03-31 patchlevel 368) [i686-linux]
    true

    $ ruby1.8 -ve 'ARGF.close; p ARGF.closed?'
    ruby 1.8.7 (2008-08-11 patchlevel 72) [i486-linux]
    true

    $ ruby -ve 'ARGF.close; p ARGF.closed?'
    ruby 1.9.2dev (2009-06-14 trunk 23689) [i686-linux]
    false

Trying to #close an already closed stream raises an IOError, whereas it didn't on 1.8. Thus, you can no longer cavalierly say ARGF.close; you must instead say ARGF.close unless ARGF.closed?, or precede it with a rescue clause.

    $ ruby86 -ve 'ARGF.close; ARGF.close' file
    ruby 1.8.6 (2009-03-31 patchlevel 368) [i686-linux]

    $ ruby1.8 -ve 'ARGF.close; ARGF.close' file
    ruby 1.8.7 (2008-08-11 patchlevel 72) [i486-linux]

    $ ruby -ve 'ARGF.close; ARGF.close' file
    ruby 1.9.2dev (2009-06-14 trunk 23689) [i686-linux]
    -e:1:in `close': closed stream (IOError)
	from -e:1:in `close'
	from -e:1:in `<main>'

This behaviour impacts ARGF#skip on 1.9, too. Worse, the error message is misleading due to refrencing 'close'; a method that the user did not invoke, thus can't be held responsible for the failure of.

    $ ruby86 -ve 'ARGF.read; ARGF.skip' file
    ruby 1.8.6 (2009-03-31 patchlevel 368) [i686-linux]
    
    $ ruby1.8 -ve 'ARGF.read; ARGF.skip' file
    ruby 1.8.7 (2008-08-11 patchlevel 72) [i486-linux]

    $ ruby -ve 'ARGF.read; ARGF.skip' file
    ruby 1.9.2dev (2009-06-14 trunk 23689) [i686-linux]
    -e:1:in `close': closed stream (IOError)
	from -e:1:in `skip'
	from -e:1:in `<main>'

Tangentially, a similarly odd exception is raised on all versions if ARGF#skip is invoked without files to skip to:

    $ ruby -ve 'ARGF.skip' file
    ruby 1.9.2dev (2009-06-14 trunk 23689) [i686-linux]
    -e:1:in `skip': undefined method `close' for nil:NilClass (NoMethodError)
	from -e:1:in `<main>'

Given that ARGF is likely to contain a variable number of files, it may be easier if these methods returned nil when there are no more files. This would retain backward compatibility, and reduce error handling logic.


----------------------------------------
http://redmine.ruby-lang.org