[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Question concerning ruby file access

Stefan Salewski

10/30/2008 12:15:00 AM

Question concerning ruby file access from a novice:

I have seen following code fragment from

http://www.rubycentral.com/pickaxe/t...

File.open("testfile", "r") do |aFile|
# ... process the file
end

Advantage of this notation is that file is automatically closed when do
loop terminates -- maybe by an exception with Process.exit.

But how do I test in this notation that the file with name "testfile"
really exits (and opening was successful). In an multi-tasking
environment another process may delete the file just before this
statement is executed.

Or in other words: I want to print a text like "File with filename
"testfile" does not exist" when opening failed.

Best regards

Stefan Salewski
3 Answers

Sebastian Hungerecker

10/30/2008 12:25:00 AM

0

Stefan Salewski wrote:
> But how do I test in this notation that the file with name "testfile"
> really exits (and opening was successful). In an multi-tasking
> environment another process may delete the file just before this
> statement is executed.

If you want to completely avoid race conditions, you don't check - you just do
and then recover from eventual errors (i.e. you rescue the exception).

HTH,
Sebastian
--
Jabber: sepp2k@jabber.org
ICQ: 205544826

Ken Bloom

10/30/2008 1:20:00 AM

0

On Thu, 30 Oct 2008 00:14:57 +0000, Stefan Salewski wrote:

> Question concerning ruby file access from a novice:
>
> I have seen following code fragment from
>
> http://www.rubycentral.com/pickaxe/t...
>
> File.open("testfile", "r") do |aFile| # ... process the file
> end
>
> Advantage of this notation is that file is automatically closed when do
> loop terminates -- maybe by an exception with Process.exit.
>
> But how do I test in this notation that the file with name "testfile"
> really exits (and opening was successful). In an multi-tasking
> environment another process may delete the file just before this
> statement is executed.
>
> Or in other words: I want to print a text like "File with filename
> "testfile" does not exist" when opening failed.
>
> Best regards
>
> Stefan Salewski

begin
File.open("testfile") do |aFile|
...
end
rescue IOError => e
#handle error
end



--
Chanoch (Ken) Bloom. PhD candidate. Linguistic Cognition Laboratory.
Department of Computer Science. Illinois Institute of Technology.
http://www.iit.edu...

Peña, Botp

10/30/2008 1:34:00 AM

0

RnJvbTogU3RlZmFuIFNhbGV3c2tpIFttYWlsdG86bWFpbEBzc2FsZXdza2kuZGVdIA0KIyBPciBp
biBvdGhlciB3b3JkczogSSB3YW50IHRvIHByaW50IGEgdGV4dCBsaWtlICJGaWxlIA0KIyB3aXRo
IGZpbGVuYW1lICJ0ZXN0ZmlsZSIgZG9lcyBub3QgZXhpc3QiIHdoZW4gb3BlbmluZyBmYWlsZWQu
IA0KDQppIHVzdWFsbHkgdGhpbmsgaXQgbGlrZSBkYidzIGxvZ2ljYWwgdW5pdCBvZiB3b3JrIG9y
IHRyYW5zYWN0aW9uLiBpIGp1c3Qgd3JhcCBpdCB3IHJ1YnkncyBiZWdpbi9lbmQgYW5kIGEgcmVz
Y3VlIGNoZWNrIGFzIG1lbnRpb25lZCBieSBzZWJhc3RpYW4uDQoNCnNvbWV0aGluZyBsaWtlIGVn
DQoNCj4gYmVnaW4NCiogRmlsZS5vcGVuKCJ0ZXN0LnJieCIpIGRvIHxmfA0KKiBlbmQNCj4gcmVz
Y3VlID0+ZQ0KPiAgIHAgZS5tZXNzYWdlDQo+IGVuZA0KIk5vIHN1Y2ggZmlsZSBvciBkaXJlY3Rv
cnkgLSB0ZXN0LnJieCINCj0+IG5pbA0KDQpub3RlLCB5b3UgY2FuIGhhdmUgbW9yZSByZXNjdWVz
IGFuZCBmaW5ldHVuZSB0aGVtLiB5b3UgY2FuIGFsc28gbWFrZSB5b3VyIG93biBleGNlcHRpb25z
Li4ganVzdCBjb250aW51ZSByZWFkaW5nIG9uIHRoZSBwaWNrYXhlLi4NCg0K