Forum: Ruby Reading empty File

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.
Df13d29fbc02d6979b4e6d3a3ccb4e4a?d=identicon&s=25 Bino Oetomo (bino)
on 2007-04-21 07:14
Hi All

I have an empty file : /mnt/ramdisk0/js01
I open it with --> f_send01 = File.open("/mnt/ramdisk0/js01","w+")

and next line is --> l_send01 = f_send01.readline
and rubby give error msg EOFError.

Question : How to tell ruby to go to next command if only the file is
not empty.

well basically what i want to do is :

-- open a file
-- if not empty , output the content , and make the file empty

regards
-bino-
Be07c8d0d6867fd9a0d525f7d17600e2?d=identicon&s=25 Damjan Rems (ther)
on 2007-04-21 08:40
> I open it with --> f_send01 = File.open("/mnt/ramdisk0/js01","w+")


I could be wrong, but aren't you opening file only for writing?

by

TheR
Df13d29fbc02d6979b4e6d3a3ccb4e4a?d=identicon&s=25 Bino Oetomo (bino)
on 2007-04-21 09:37
Damjan Rems wrote:
>
>
> I could be wrong, but aren't you opening file only for writing?
>

I thing that "w+" flag means "read and write", Am I right ?

Regards
-bino-
Df13d29fbc02d6979b4e6d3a3ccb4e4a?d=identicon&s=25 Bino Oetomo (bino)
on 2007-04-21 09:43
Bino Oetomo wrote:
>
> I thing that "w+" flag means "read and write", Am I right ?
>

OOOpppsss
I just realize that i have to use "r+" for read write without "truncate"

Regards
-bino-
836f978bce10ce590ed2d38c5f56eb1d?d=identicon&s=25 Philipp Taprogge (Guest)
on 2007-04-22 16:26
(Received via mailing list)
Hi!

Thus spake Bino Oetomo on 04/21/2007 07:14 AM:
> Question : How to tell ruby to go to next command if only the file is
> not empty.

You have two options: either you rescue the EOFError and deal with
it programmatically, or you just use IO.gets instead of IO.readline,
which will not raise an error but return 'nil' if the EOF is reached.

HTH,

  Phil
Df13d29fbc02d6979b4e6d3a3ccb4e4a?d=identicon&s=25 Bino Oetomo (bino)
on 2007-04-23 02:51
Hi All
Philipp Taprogge wrote:
> Hi!
>
> Thus spake Bino Oetomo on 04/21/2007 07:14 AM:
>> Question : How to tell ruby to go to next command if only the file is
>> not empty.
>
> You have two options: either you rescue the EOFError and deal with
> it programmatically, or you just use IO.gets instead of IO.readline,
> which will not raise an error but return 'nil' if the EOF is reached.
>
> HTH,
>
>   Phil

I tried it with irb, here is the cut
-----------Start--------------
[root@kannel root]# rm /mnt/ramdisk0/js01
rm: remove regular empty file `/mnt/ramdisk0/js01'? y
[root@kannel root]# touch /mnt/ramdisk0/js01
[root@kannel root]# irb
irb(main):001:0> File.open("/mnt/ramdisk0/js01", "r+") do |f|
irb(main):002:1*     f.each_line { |line|
irb(main):003:2* puts line
irb(main):004:2> f.close}
irb(main):005:1>     rescue EOFError
irb(main):006:1> f.close
irb(main):007:1> end
SyntaxError: compile error
(irb):5: parse error, unexpected kRESCUE, expecting kEND
    rescue EOFError
          ^
        from (irb):7
        from :0
----------end-----------------
807270f56f26ad90755eef71f2c228fe?d=identicon&s=25 Alex Gutteridge (Guest)
on 2007-04-23 03:01
(Received via mailing list)
On 23 Apr 2007, at 09:51, Bino Oetomo wrote:

>> it programmatically, or you just use IO.gets instead of IO.readline,
> [root@kannel root]# touch /mnt/ramdisk0/js01
>     rescue EOFError
>           ^
>         from (irb):7
>         from :0
> ----------end-----------------
>
>
> --
> Posted via http://www.ruby-forum.com/.
>

rescue needs to be paired with a begin. E.g.

irb(main):021:0> begin
irb(main):022:1* raise
irb(main):023:1> rescue
irb(main):024:1> puts "hi"
irb(main):025:1> end
hi
=> nil

Alex Gutteridge

Bioinformatics Center
Kyoto University
This topic is locked and can not be replied to.