Forum: Ruby open file for reading as non block

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Junkone (Guest)
on 2008-10-06 03:48
(Received via mailing list)
how do i open the file for reading as non blocking mode. as you can
see the first 2 attempts do not work.

irb(main):058:0> File.open(fi,File::NONBLOCK + File::RDONLY){|f|
c=f.read}
IOError: not opened for reading
        from (irb):58:in `read'
        from (irb):58
        from (irb):58:in `open'
        from (irb):58
        from ¢¾:0
irb(main):059:0> File.open(fi,File::NONBLOCK ){|f| c=f.read}
IOError: not opened for reading
        from (irb):59:in `read'
        from (irb):59
        from (irb):59:in `open'
        from (irb):59
        from ¢¾:0
irb(main):060:0> File.open(fi,"r" ){|f| c=f.read}
=> "test"
irb(main):061:0>
Robert K. (Guest)
on 2008-10-06 15:17
(Received via mailing list)
2008/10/5 Junkone <removed_email_address@domain.invalid>:
>        from ¢¾:0
Works for me:

irb(main):006:0> File.open("environment", File::RDONLY |
File::NONBLOCK) {|io| io.read}.size
=> 4398

IMHO "|" is better than "+" because it expresses more clearly that
this is about bit masks.

Btw, what do you expect from opening a file nonblocking?  Is there any
other process writing to it?

Cheers

robert
This topic is locked and can not be replied to.