File.open auto-close

Discussion in 'Ruby' started by Sammy Larbi, May 29, 2008.

  1. Sammy Larbi

    Sammy Larbi Guest

    [Note: parts of this message were removed to make it a legal post.]

    I know opening a file and passing a block will execute the block in context
    of the file and then close the file.

    Example:

    File.open("somefile.txt") {|f| the_text = f.read }

    After running, somefile.txt will be closed.

    I'm wondering if it's ok to read a file like this:

    the_text = File.open("somefile.txt").read

    Will that close automatically? If not, how would I go about closing it?

    Thanks,
    Sam
     
    Sammy Larbi, May 29, 2008
    #1
    1. Advertising

  2. Tm8sIGZpbGUgd29uJ3QgYmUgY2xvc2VkICh5b3UgZGlkbid0IHBhc3MgYmxvY2spIGFuZCB5b3Ug
    Y2FuJ3QgZG8gaXQKYmVjYXVzZSB5b3UgZG9uJ3QgaGF2ZSByZWZlcmVuY2UgdG8gZmlsZSAobG9z
    dCB3aGVuIGNoYWluaW5nKS4KQnV0IHRoZXJlIGlzIGNsYXNzIG1ldGhvZCBGaWxlLnJlYWQgc28g
    eW91IGNhbjoKCnRoZV90ZXh0ID0gRmlsZS5yZWFkKCJzb21lZmlsZS50eHQiKQoKOi0pCgotLSAK
    UmFkb3OzYXcgQnWzYXQKCmh0dHA6Ly9yYWRhcmVrLmpvZ2dlci5wbCAtIG3zaiBibG9nCg==
     
    Rados³aw Bu³at, May 29, 2008
    #2
    1. Advertising

  3. It's even easier :) Try

    the_text = File.read("somefile.txt")

    Sammy Larbi wrote:
    > I know opening a file and passing a block will execute the block in context
    > of the file and then close the file.
    >
    > Example:
    >
    > File.open("somefile.txt") {|f| the_text = f.read }
    >
    > After running, somefile.txt will be closed.
    >
    > I'm wondering if it's ok to read a file like this:
    >
    > the_text = File.open("somefile.txt").read
    >
    > Will that close automatically? If not, how would I go about closing it?
    >
    > Thanks,
    > Sam
    >
    >
     
    Bryan JJ Buckley, May 29, 2008
    #3
  4. Sammy Larbi

    Sammy Larbi Guest

    MjAwOC81LzI5IFJhZG9zs2F3IEJ1s2F0IDxyYWRlay5idWxhdEBnbWFpbC5jb20+OgoKPiBObywg
    ZmlsZSB3b24ndCBiZSBjbG9zZWQgKHlvdSBkaWRuJ3QgcGFzcyBibG9jaykgYW5kIHlvdSBjYW4n
    dCBkbyBpdAo+IGJlY2F1c2UgeW91IGRvbid0IGhhdmUgcmVmZXJlbmNlIHRvIGZpbGUgKGxvc3Qg
    d2hlbiBjaGFpbmluZykuCj4gQnV0IHRoZXJlIGlzIGNsYXNzIG1ldGhvZCBGaWxlLnJlYWQgc28g
    eW91IGNhbjoKPgo+IHRoZV90ZXh0ID0gRmlsZS5yZWFkKCJzb21lZmlsZS50eHQiKQo+Cj4KCkR1
    aGhoaC4gIEknbSBhIG1vcm9uLiBJIG5ldmVyIHRob3VnaHQgb2YgdGhhdC4KCgpUaGFua3MuCgpJ
    bmNpZGVudGFsbHksIEkgbm90aWNlIHRoZSBkb2NzIG9uIHJ1YnktZG9jIGFyZSBzdGlsbCBicm9r
    ZW4gZm9yIEZpbGUgY2xhc3MsCndoaWNoIGlzIHRoZSBmaXJzdCBwbGFjZSBJIHRyaWVkIHRvIGxv
    b2suID0pCgpUaGFua3MgYWdhaW4uCgpTYW0K
     
    Sammy Larbi, May 29, 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. Maziar Aflatoun

    Database update with auto connection open/close

    Maziar Aflatoun, Aug 11, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    541
    tom pester
    Aug 11, 2005
  2. linkswanted
    Replies:
    1
    Views:
    949
  3. Dani
    Replies:
    3
    Views:
    491
  4. Iñaki Baz Castillo
    Replies:
    7
    Views:
    896
    Iñaki Baz Castillo
    Jan 12, 2010
  5. Iulian Ilea
    Replies:
    1
    Views:
    326
    pcx99
    Dec 21, 2006
Loading...

Share This Page