Question concerning ruby file access

S

Stefan Salewski

Question concerning ruby file access from a novice:

I have seen following code fragment from

http://www.rubycentral.com/pickaxe/tut_io.html

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
 
S

Sebastian Hungerecker

Stefan said:
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
 
K

Ken Bloom

Question concerning ruby file access from a novice:

I have seen following code fragment from

http://www.rubycentral.com/pickaxe/tut_io.html

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
 
P

Peña, Botp

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top