Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > File.open auto-close

Reply
Thread Tools

File.open auto-close

 
 
Sammy Larbi
Guest
Posts: n/a
 
      05-29-2008
[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

 
Reply With Quote
 
 
 
 
Radosław Bułat
Guest
Posts: n/a
 
      05-29-2008
Tm8sIGZpbGUgd29uJ3QgYmUgY2xvc2VkICh5b3UgZGlkbid0IH Bhc3MgYmxvY2spIGFuZCB5b3Ug
Y2FuJ3QgZG8gaXQKYmVjYXVzZSB5b3UgZG9uJ3QgaGF2ZSByZW ZlcmVuY2UgdG8gZmlsZSAobG9z
dCB3aGVuIGNoYWluaW5nKS4KQnV0IHRoZXJlIGlzIGNsYXNzIG 1ldGhvZCBGaWxlLnJlYWQgc28g
eW91IGNhbjoKCnRoZV90ZXh0ID0gRmlsZS5yZWFkKCJzb21lZm lsZS50eHQiKQoKOi0pCgotLSAK
UmFkb3OzYXcgQnWzYXQKCmh0dHA6Ly9yYWRhcmVrLmpvZ2dlci 5wbCAtIG3zaiBibG9nCg==

 
Reply With Quote
 
 
 
 
Bryan JJ Buckley
Guest
Posts: n/a
 
      05-29-2008
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
>
>


 
Reply With Quote
 
Sammy Larbi
Guest
Posts: n/a
 
      05-29-2008
MjAwOC81LzI5IFJhZG9zs2F3IEJ1s2F0IDxyYWRlay5idWxhdE BnbWFpbC5jb20+OgoKPiBObywg
ZmlsZSB3b24ndCBiZSBjbG9zZWQgKHlvdSBkaWRuJ3QgcGFzcy BibG9jaykgYW5kIHlvdSBjYW4n
dCBkbyBpdAo+IGJlY2F1c2UgeW91IGRvbid0IGhhdmUgcmVmZX JlbmNlIHRvIGZpbGUgKGxvc3Qg
d2hlbiBjaGFpbmluZykuCj4gQnV0IHRoZXJlIGlzIGNsYXNzIG 1ldGhvZCBGaWxlLnJlYWQgc28g
eW91IGNhbjoKPgo+IHRoZV90ZXh0ID0gRmlsZS5yZWFkKCJzb2 1lZmlsZS50eHQiKQo+Cj4KCkR1
aGhoaC4gIEknbSBhIG1vcm9uLiBJIG5ldmVyIHRob3VnaHQgb2 YgdGhhdC4KCgpUaGFua3MuCgpJ
bmNpZGVudGFsbHksIEkgbm90aWNlIHRoZSBkb2NzIG9uIHJ1Yn ktZG9jIGFyZSBzdGlsbCBicm9r
ZW4gZm9yIEZpbGUgY2xhc3MsCndoaWNoIGlzIHRoZSBmaXJzdC BwbGFjZSBJIHRyaWVkIHRvIGxv
b2suID0pCgpUaGFua3MgYWdhaW4uCgpTYW0K
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off




Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57