Empty file returns nil not empty string?

Hello,

In ruby-1.6.7 reading an empty file returns an empty string before returning
nil. 1.7.2 doesn’t do that - why?

Thomas

In ruby-1.6.7 reading an empty file returns an empty string before returning
nil. 1.7.2 doesn't do that - why?

Can you give an example ?

pigeon% ls -al aa
-rw-r--r-- 1 ts ts 0 Aug 11 15:06 aa
pigeon%

pigeon% ruby -ve 'a = File.open("aa"); while b = a.gets; p b; end'
ruby 1.6.7 (2002-03-01) [i686-linux]
pigeon%

Guy Decoux

Sure - here’s a test case:

require ‘test/unit’

class TestFileRead < Test::Unit::TestCase
def test_read_empty
touch empty.txt
assert_equal(0, File.size(“empty.txt”))
File.open(“empty.txt”) {
>f>
assert_equal("", f.read)
assert_equal(nil, f.read)
}
end
end

Here’s the output

···

===========================
$ /C/ruby167/bin/ruby readtest.rb
Loaded suite readtest
Started…

Finished in 0.13 seconds.
1 runs, 3 assertions, 0 failures, 0 errors

$ /C/ruby172/bin/ruby readtest.rb
c:/ruby172/lib/ruby/site_ruby/1.7/test/unit/error.rb:35: warning: escaped
terminator ‘"’ inside string interpolation
Loaded suite readtest
Started…

Failure occurred in test_read_empty(TestFileRead) [readtest.rb:12]: Expected
<> but was

Finished in 0.02 seconds.
1 runs, 2 assertions, 1 failures, 0 errors

Cheers,

Thomas

In ruby-1.6.7 reading an empty file returns an empty string before
returning

nil. 1.7.2 doesn’t do that - why?

Can you give an example ?

pigeon% ls -al aa
-rw-r–r-- 1 ts ts 0 Aug 11 15:06 aa
pigeon%

pigeon% ruby -ve 'a = File.open(“aa”); while b = a.gets; p b; end’
ruby 1.6.7 (2002-03-01) [i686-linux]
pigeon%

Guy Decoux

Oh, and I’m using Andy’s latest windows installers. The versions are:

$ /C/ruby167/bin/ruby --version
ruby 1.6.7 (2002-03-01) [i586-mswin32]

$ /C/ruby172/bin/ruby --version
ruby 1.7.2 (2002-07-02) [i386-mswin32]

Thomas

      assert_equal("", f.read)

The modification is perhaps

···

Sat Mar 23 01:50:30 2002 Yukihiro Matsumoto <matz@ruby-lang.org>

        * io.c (read_all): files on /proc filesystem with zero stat size,
          may have contents.

1.6.7 call stat(), if the size == 0 it return "" and force an eof

1.7.2 don't call stat() and it return nil when read.size == 0 and it's at
eof

Guy Decoux

I've said a stupidity

1.7.2 don't call stat()

1.7.2 call stat and in this case ruby use the default value of 8192 to
trying to read the file.

Guy Decoux

I’ve said a stupidity

1.7.2 don’t call stat()

1.7.2 call stat and in this case ruby use the default value of 8192 to
trying to read the file.

I don’t understand what you mean by “… use the default value of 8192 to
trying to read the file”. Can you explain it in a little more detail?

Thomas

I don't understand what you mean by "... use the default value of 8192 to
trying to read the file". Can you explain it in a little more detail?

For 1.6.7 if the result of fstat == 0, ruby return "" and force eof
For 1.7.2 if the result of fstat == 0, ruby will try to read from the file
with a buffer of 8192 bytes (call to fread())

Guy Decoux