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.
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-----------------
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-----------------
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.