empty file returns nil not empty string? 
Author Message
 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



Thu, 27 Jan 2005 21:10:54 GMT  
 empty file returns nil not empty string?

Quote:
>>>>> "T" == =?iso-8859-1?Q?Thomas S=F8ndergaard?= <iso-8859-1> writes:

T> In ruby-1.6.7 reading an empty file returns an empty string before returning
T> 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



Thu, 27 Jan 2005 21:21:14 GMT  
 empty file returns nil not empty string?
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 <nil>

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

Cheers,

Thomas

Quote:
> >>>>> "T" == =?iso-8859-1?Q?Thomas S=F8ndergaard?= <iso-8859-1> writes:

> T> In ruby-1.6.7 reading an empty file returns an empty string before
returning
> T> 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



Thu, 27 Jan 2005 21:48:54 GMT  
 empty file returns nil not empty string?
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



Thu, 27 Jan 2005 21:51:43 GMT  
 empty file returns nil not empty string?

Quote:
>>>>> "T" == =?iso-8859-1?Q?Thomas S=F8ndergaard?= <iso-8859-1> writes:

T>       assert_equal("", f.read)

 The modification is perhaps


        * 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



Thu, 27 Jan 2005 22:04:41 GMT  
 empty file returns nil not empty string?

 I've said a stupidity

t>  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



Thu, 27 Jan 2005 22:21:19 GMT  
 empty file returns nil not empty string?

Quote:
>  I've said a stupidity

> t>  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



Thu, 27 Jan 2005 22:47:07 GMT  
 empty file returns nil not empty string?

Quote:
>>>>> "T" == =?iso-8859-1?Q?Thomas S=F8ndergaard?= <iso-8859-1> writes:

T> I don't understand what you mean by "... use the default value of 8192 to
T> 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



Thu, 27 Jan 2005 22:55:36 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. do NOT download, this file is EMPTY - empty.txt (1/1)

2. empty Array returned but expect nil

3. Array#sort! returns nil when array empty

4. file.read() returns empty string prematurely.

5. file.read(num) returns empty string before EOF.

6. Empty DB, yet not empty...

7. question: reading an empty file results in nil

8. string.split returns empty when unaccessed

9. returns empty string?

10. \0 is not he empty string anymore - was in 8.0

11. auto_mkindex returns empty file

12. os.listdir ('.'): Solaris: returns empty list in directory with 215 files

 

 
Powered by phpBB® Forum Software