Question concerning ruby file access

Discussion in 'Ruby' started by Stefan Salewski, Oct 30, 2008.

  1. 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
    Stefan Salewski, Oct 30, 2008
    #1
    1. Advertising

  2. 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:
    ICQ: 205544826
    Sebastian Hungerecker, Oct 30, 2008
    #2
    1. Advertising

  3. Stefan Salewski

    Ken Bloom Guest

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



    --
    Chanoch (Ken) Bloom. PhD candidate. Linguistic Cognition Laboratory.
    Department of Computer Science. Illinois Institute of Technology.
    http://www.iit.edu/~kbloom1/
    Ken Bloom, Oct 30, 2008
    #3
  4. RnJvbTogU3RlZmFuIFNhbGV3c2tpIFttYWlsdG86bWFpbEBzc2FsZXdza2kuZGVdIA0KIyBPciBp
    biBvdGhlciB3b3JkczogSSB3YW50IHRvIHByaW50IGEgdGV4dCBsaWtlICJGaWxlIA0KIyB3aXRo
    IGZpbGVuYW1lICJ0ZXN0ZmlsZSIgZG9lcyBub3QgZXhpc3QiIHdoZW4gb3BlbmluZyBmYWlsZWQu
    IA0KDQppIHVzdWFsbHkgdGhpbmsgaXQgbGlrZSBkYidzIGxvZ2ljYWwgdW5pdCBvZiB3b3JrIG9y
    IHRyYW5zYWN0aW9uLiBpIGp1c3Qgd3JhcCBpdCB3IHJ1YnkncyBiZWdpbi9lbmQgYW5kIGEgcmVz
    Y3VlIGNoZWNrIGFzIG1lbnRpb25lZCBieSBzZWJhc3RpYW4uDQoNCnNvbWV0aGluZyBsaWtlIGVn
    DQoNCj4gYmVnaW4NCiogRmlsZS5vcGVuKCJ0ZXN0LnJieCIpIGRvIHxmfA0KKiBlbmQNCj4gcmVz
    Y3VlID0+ZQ0KPiAgIHAgZS5tZXNzYWdlDQo+IGVuZA0KIk5vIHN1Y2ggZmlsZSBvciBkaXJlY3Rv
    cnkgLSB0ZXN0LnJieCINCj0+IG5pbA0KDQpub3RlLCB5b3UgY2FuIGhhdmUgbW9yZSByZXNjdWVz
    IGFuZCBmaW5ldHVuZSB0aGVtLiB5b3UgY2FuIGFsc28gbWFrZSB5b3VyIG93biBleGNlcHRpb25z
    Li4ganVzdCBjb250aW51ZSByZWFkaW5nIG9uIHRoZSBwaWNrYXhlLi4NCg0K
    Peña, Botp, Oct 30, 2008
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kevin Spencer

    Re: Question concerning Timer Control

    Kevin Spencer, Sep 21, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    399
    Kevin Spencer
    Sep 21, 2004
  2. =?Utf-8?B?U3RhbmxleSBBbGV4?=

    Question concerning Timer Control

    =?Utf-8?B?U3RhbmxleSBBbGV4?=, Sep 21, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    309
    =?Utf-8?B?U3RhbmxleSBBbGV4?=
    Sep 21, 2004
  3. djc
    Replies:
    2
    Views:
    540
  4. Oliver Andrich
    Replies:
    6
    Views:
    117
    James Edward Gray II
    Dec 20, 2005
  5. Michael Litchard

    Newbie question concerning ruby and mysql

    Michael Litchard, Jun 6, 2008, in forum: Ruby
    Replies:
    7
    Views:
    135
    Michael Litchard
    Jun 6, 2008
Loading...

Share This Page