File.read not returning nil [newbie]

N

Navindra Umanee

Hi,

If I run the following code:

# "empty" is a 0-byte file in the current directory

File.open("empty") { |file|
print "file.read should return nil\n" if file.eof?
print "file.read: "
print file.read
print "\n"
}

read = File.open("empty").read
print "read is NOT nil\n" if read
--------------------------------------

I get:

file.read should return nil
file.read: nil
read is NOT nil

This is completely baffling me. Can anyone explain what is going on?
I apologise if this is an obvious question, any pointers would be
appreciated.

Thanks,
Navin.
 
F

Florian Gross

Navindra said:
file.read should return nil
file.read: nil
read is NOT nil

This is completely baffling me. Can anyone explain what is going on?
I apologise if this is an obvious question, any pointers would be
appreciated.

irb(main):009:0> File.open("NUL") { |f| f.read }
=> ""

It is not returning nil, it returns an empty String instead. Probably
best to use p() or IRB when you're checking things like this. There's
also File.read(fn) which might help with simplifying code.
 
N

Navindra Umanee

Hi Florian,

Thanks a lot for your response!

Florian Gross said:
irb(main):009:0> File.open("NUL") { |f| f.read }
=> ""

It is not returning nil, it returns an empty String instead. Probably
best to use p() or IRB when you're checking things like this. There's
also File.read(fn) which might help with simplifying code.

I am completely confused. The documentation says it should print nil:

http://www.rubycentral.com/book/ref_c_io.html#IO.read

It does return nil in my first test case. It does not return nil in
my second test case which is identical to your test case and as far as
I can see shows the same problem.

So why the difference in behavior? It's still completely baffling me.

Thanks again for your help. I am hoping I can get a proof-of-concept
replacement for dot.kde.org up some time in the distant future. If
it's successful, this would be another high profile site running on
Ruby.

Cheers,
Navin.
 
F

Florian Gross

Navindra said:
Hi Florian,
Moin.


I am completely confused. The documentation says it should print nil:

http://www.rubycentral.com/book/ref_c_io.html#IO.read

It does return nil in my first test case. It does not return nil in
my second test case which is identical to your test case and as far as
I can see shows the same problem.

So why the difference in behavior? It's still completely baffling me.

Hm, it seems to return nil when you specify a count (even if you use 0)
argument. I'm not sure why it's like that. Maybe matz or somebody else
who has knowledge on this could answer...
Thanks again for your help. I am hoping I can get a proof-of-concept
replacement for dot.kde.org up some time in the distant future. If
it's successful, this would be another high profile site running on
Ruby.

That would indeed be very nice. I'm looking forward to this. :)
Cheers,
Navin.

Regards,
Florian Gross
 
T

Tim Heaney

Navindra Umanee said:
I am completely confused. The documentation says it should print nil:

http://www.rubycentral.com/book/ref_c_io.html#IO.read

It does return nil in my first test case.

It doesn't for me. I get the empty string both times. But why?

It looks like there was a similar discussion in August of 2002

http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-dev/18089

but I don't know how to read Japanese. Nonetheless, I clearly get a
different result than the one shown there

$ ruby -e 'p IO.read("/dev/null")'
""
$ ruby -v
ruby 1.8.2 (2004-12-25) [i686-linux]
 
N

Navindra Umanee

Tim Heaney said:
It doesn't for me. I get the empty string both times. But why?

I guess that's better than being inconsistent. nil would have been
nicer, IMHO. Do you know if there is a way to make "" equivalent to
nil? I guess I could try messing around with the NilClass.

Just so that I'm clear. You tried this:

File.open("/dev/null") { |file|
print "file.read should return nil\n" if file.eof?
print "file.read: "
print file.read
print "\n"
}

and got it to print the empty string?
$ ruby -e 'p IO.read("/dev/null")'
""

Same here.

Thanks,
Navin.
 
T

Tanaka Akira

Florian Gross said:
Hm, it seems to return nil when you specify a count (even if you use 0)
argument. I'm not sure why it's like that. Maybe matz or somebody else
who has knowledge on this could answer...

io.read() and io.read(nil) returns always a string which may be empty.

io.read(n) with 0<n returns a non-empty string or nil.

io.read(0) return "" or nil in Ruby 1.8.
It returns always "" in Ruby 1.9.
 
N

Navindra Umanee

Florian Gross said:
That would indeed be very nice. I'm looking forward to this. :)

I guess I might as well ask you this here. :)

Any idea why the second one fails:
DateTime.strptime('2000-09-28 05:23:01 -0400', '%F %T %z').strftime('%s')
=> "970132981"
DateTime.strptime('970132981', '%s').strftime('%s')
ArgumentError: invalid date
from /usr/lib/ruby/1.8/date.rb:485:in `new_with_hash'
from /usr/lib/ruby/1.8/date.rb:495:in `strptime'
from (irb):31

Thanks!
Navin.
 
T

Tim Heaney

Navindra Umanee said:
Just so that I'm clear. You tried this:

File.open("/dev/null") { |file|
print "file.read should return nil\n" if file.eof?
print "file.read: "
print file.read
print "\n"
}

and got it to print the empty string?

That's right, yes.

$ cat fileread.rb
#!/usr/local/bin/ruby -w

File.open("/dev/null") { |file|
print "file.read should return nil\n" if file.eof?
print "file.read: "
print file.read
print "\n"
}
$ ./fileread.rb
file.read should return nil
file.read:
 
N

Navindra Umanee

Tim Heaney said:
That's right, yes.

Ah, okay. I guess my Ruby is too old:

[vinata] [/tmp] ruby -v
ruby 1.8.0 (2003-08-04) [i586-linux-gnu]

Looks like the behaviour changed in 1.8.1.

Thanks,
Navin.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top