[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Reading empty File

bino_oetomo

4/21/2007 5:14:00 AM

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-

--
Posted via http://www.ruby-....

19 Answers

Damjan Rems

4/21/2007 6:41:00 AM

0


> 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

--
Posted via http://www.ruby-....

bino_oetomo

4/21/2007 7:38:00 AM

0

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-

--
Posted via http://www.ruby-....

bino_oetomo

4/21/2007 7:43:00 AM

0

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-

--
Posted via http://www.ruby-....

Philipp Taprogge

4/22/2007 2:22:00 PM

0

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

bino_oetomo

4/23/2007 12:51:00 AM

0

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


--
Posted via http://www.ruby-....

Alex Gutteridge

4/23/2007 1:01:00 AM

0

On 23 Apr 2007, at 09:51, Bino Oetomo wrote:

> 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-----------------
>
>
> --
> Posted via http://www.ruby-....
>

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



Kurt Bashwitz

11/6/2011 11:48:00 AM

0

On Nov 6, 10:48 am, snip crap <..@.com> wrote:

is your name *snip crap* ?

Mike Lovell

11/6/2011 7:09:00 PM

0

On 2011-11-06, George Hammond MSc <Nowhere1@notspam.com> wrote:
> Look, Wilson, you and me are the only discussants on this
> thread who know what the Einstein curvature tensor, Guv,
> actually is.
> I have discovered, proved and published the fact that:
>
> God = Guv (of subjective spacetime)

I agree you've done 1 and 3 ;-)

Question, would anything anyone could say about the paper (still haven't
had any more details on the journal from you which seems to share the
name with another well known quack journal) change your mind about it?
And cause you to reject your own findings?


If the answer is no, there's no point discussing it. Your mind is made
up.

--
Jews, Christians & Muslims
The content of your posts will show how much you
really believe God is looking over your shoulder

George Hammond

11/6/2011 7:44:00 PM

0

On Sun, 06 Nov 2011 09:01:17 +1100, ..@..(Henry Wilson DSc.)
wrote:

[Hammond]
It doesn't take much to arouse my suspicion in normal
circumstances, however in casual Usenet situations I may not
bother to vet someone until after they say something rather
than before.
After my post to "Mr. DSc." below however, I decided to
check his bona fides and immediately of course found out
he's a well known Internet crank named Ralph Rabbidge, who
according to P. B. Anderson and many others, is a "senile
Australian farmer calling himself by the fake name and title
" Henry Wilson DSc.""
Apparently Mr. Rabbidge spends most of his time arguing
SR with other scientific amateurs and doubtless knows
absolutely nothing about General Relativity and even less
about God.
Christ... I should have known.... as if you could run
into a competent scientist, besides me, on an unmoderated
Usenet newsgroup...!
========================================
GEORGE HAMMOND'S PROOF OF GOD WEBSITE
Primary site
http://webspace.webring.com/people/eg/geor...
Mirror site
http://proof-of-god.freewebsiteh...
HAMMOND FOLK SONG by Casey Bennetto
http://interrobang.jwgh.org/songs/h...
=======================================

George Hammond

11/6/2011 8:01:00 PM

0

On Sun, 06 Nov 2011 13:09:25 -0600, Mike Lovell
<mike.lovell@null.local> wrote:

>On 2011-11-06, George Hammond MSc <Nowhere1@notspam.com> wrote:
>> Look, Wilson, you and me are the only discussants on this
>> thread who know what the Einstein curvature tensor, Guv,
>> actually is.
>> I have discovered, proved and published the fact that:
>>
>> God = Guv (of subjective spacetime)
>
>I agree you've done 1 and 3 ;-)
>
>Question, would anything anyone could say about the paper (still haven't
>had any more details on the journal from you which seems to share the
>name with another well known quack journal) change your mind about it?
>And cause you to reject your own findings?
>
>
>If the answer is no, there's no point discussing it. Your mind is made
>up.
>
[Hammond]
I don't waste my breath talking to scientifically
unqualified amateurs.
Turns out <Henry Wilson DSc.> is a fake... a crackpot
Austrailian ranch hand named "Ralph Rabbidge" who is not a
DSc. and apparently wouldn't have the slightest idea what
Guv, or General Relativity is, much less what God is.
And furthermore neither would you for that matter!
========================================
GEORGE HAMMOND'S PROOF OF GOD WEBSITE
Primary site
http://webspace.webring.com/people/eg/geor...
Mirror site
http://proof-of-god.freewebsiteh...
HAMMOND FOLK SONG by Casey Bennetto
http://interrobang.jwgh.org/songs/h...
=======================================