When file gets closed in a block

Discussion in 'Ruby' started by Damjan Rems, Jun 19, 2007.

  1. Damjan Rems

    Damjan Rems Guest

    I like to use this a lot:
    File.new(cName,'wb+').write('aText')

    I have understood that file gets automaticly closed. But when? You can
    not delete file right after above statement. If you want to delete file
    you must use this.
    f = File.new(cName,'wb+')
    f.write(attachment.unpack('m'))
    f.close

    When does the file in block gets closed.

    by
    TheR

    --
    Posted via http://www.ruby-forum.com/.
    Damjan Rems, Jun 19, 2007
    #1
    1. Advertising

  2. Damjan Rems

    Sascha Abel Guest

    If you open a file with File#new, you have to close it yourself by
    calling File#close.

    ---
    f = File.new('someFilename', 'w')
    f.write(text1)
    f.write(text2)
    f.close
    ---

    You can write as often as you like to f as long as you don't close it.

    File#open closes the file automatically if passed a block:

    ---
    File.open('someFilename', 'w') {|f|
    f.write(text1)
    f.write(text2)
    }
    ---

    Without a block File#open ist just an alias for File#new.


    Sascha

    --
    Posted via http://www.ruby-forum.com/.
    Sascha Abel, Jun 19, 2007
    #2
    1. Advertising

  3. Damjan Rems

    Damjan Rems Guest

    >
    > You can write as often as you like to f as long as you don't close it.
    >
    > File#open closes the file automatically if passed a block:
    >
    > ---
    > File.open('someFilename', 'w') {|f|
    > f.write(text1)
    > f.write(text2)
    > }


    I guess then my one liner should look like this:

    File.new(cName,'wb+') { |f|.write('aText') }

    by
    TheR

    --
    Posted via http://www.ruby-forum.com/.
    Damjan Rems, Jun 19, 2007
    #3
  4. On Jun 19, 2007, at 5:52 AM, Damjan Rems wrote:
    >> You can write as often as you like to f as long as you don't close
    >> it.
    >>
    >> File#open closes the file automatically if passed a block:
    >>
    >> ---
    >> File.open('someFilename', 'w') {|f|
    >> f.write(text1)
    >> f.write(text2)
    >> }

    >
    > I guess then my one liner should look like this:
    >
    > File.new(cName,'wb+') { |f|.write('aText') }
    >
    > by
    > TheR


    Close, try it like this:

    File.open(cName,'wb+') { |f| f.write('aText') }

    -Rob

    Rob Biedenharn http://agileconsultingllc.com
    Rob Biedenharn, Jun 19, 2007
    #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. Stefan Mueller
    Replies:
    5
    Views:
    517
    Steven Saunderson
    Jul 10, 2006
  2. morrell
    Replies:
    1
    Views:
    946
    roy axenov
    Oct 10, 2006
  3. SAL
    Replies:
    5
    Views:
    478
    Steven Cheng
    Oct 1, 2008
  4. John Joyce

    gets gets

    John Joyce, Mar 26, 2007, in forum: Ruby
    Replies:
    2
    Views:
    348
    John Joyce
    Mar 26, 2007
  5. Matt Kruse
    Replies:
    5
    Views:
    307
    Richard Cornford
    Sep 9, 2003
Loading...

Share This Page