# How do I ensure that File.write finished?

**URL:** <https://www.ruby-forum.com/t/how-do-i-ensure-that-file-write-finished/180053>\
**Category:** Ruby\
**Created:** [December 10, 2009, 12:18pm UTC](https://www.ruby-forum.com/t/how-do-i-ensure-that-file-write-finished/180053 "2009-12-10T12:18:51Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![emuen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/96bed5/32.png) [@emuen](https://www.ruby-forum.com/u/emuen)\
**Post date:** [December 10, 2009, 12:18pm UTC](https://www.ruby-forum.com/t/how-do-i-ensure-that-file-write-finished/180053/1 "2009-12-10T12:18:51Z")

</div>

Hi there.

I am trying to make a form to update the values in a yml file (my  
applications configuration file). This is working kind of fine.  
Sometimes it works, sometimes not.

I have confirmed (through logging) that a content for +string+ is always  
there. So there is always something trying to be written into the yml  
file, but still sometimes it ends up being empty. And an empty  
configuration file is no good!

```auto
# server-controller (user generated controller in rails)
  def update
    # This inserts a new setting into the hash
    unless params[:new_setting][:key].blank? and
params[:new_setting][:value].blank?
      params[:settings].collect{ |env, settings|
settings[params[:new_setting][:key].to_sym] =
params[:new_setting][:value].to_s }
    end

    # This writes the hash to the file (See the second snippet for
details)
    ServerSystem::update_configuration(params[:settings])

    # Redirects to the action that restarts the server to load the new
configurations
    redirect_to admin_server_restart_path
  end
```

```auto
# Custom class
    def update_configuration(settings)
      string = settings.to_yaml
      File.new(APP_CONFIG_FILE, "w").write string
    end
```

The problem is that the setup is dodgy, sometimes it works, sometimes it  
looks like the server is restarted before the content of +string+ is  
written to the configuration file.  
How can I ensure that the content has been written before executing next  
line of code (the restart in this case)?

---

<div class="post-metadata">

**Author:** ![emuen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/96bed5/32.png) [@emuen](https://www.ruby-forum.com/u/emuen)\
**Post date:** [December 10, 2009, 12:58pm UTC](https://www.ruby-forum.com/t/how-do-i-ensure-that-file-write-finished/180053/2 "2009-12-10T12:58:59Z")

</div>

On Thursday 10 December 2009, Emil K. wrote:

> |  
> |  
> |[code]  
> |How can I ensure that the content has been written before executing next  
> |line of code (the restart in this case)?  
> |

I’m not sure, but maybe the fact you’re not closing the file after  
writing has  
something to do with that. Try replacing the last line of  
update\_configuration  
with:

File.open(APP\_CONFIG\_FILE, ‘w’){|f| f.write string}

When given a block, File.open closes the file after calling the block,  
which  
should ensure that everything you wrote to file should have actually  
been  
written.

I hope this helps

Stefano

---

<div class="post-metadata">

**Author:** ![emuen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/96bed5/32.png) [@emuen](https://www.ruby-forum.com/u/emuen)\
**Post date:** [December 10, 2009, 2:34pm UTC](https://www.ruby-forum.com/t/how-do-i-ensure-that-file-write-finished/180053/3 "2009-12-10T14:34:58Z")

</div>

2009/12/10 Stefano C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> > |configuration file is no good!  
> > | end  
> > |  
> > |written to the configuration file.  
> > |How can I ensure that the content has been written before executing next  
> > |line of code (the restart in this case)?  
> > |
> 
> I’m not sure, but maybe the fact you’re not closing the file after writing has  
> something to do with that.

That’s definitively the reason.

> Try replacing the last line of update\_configuration  
> with:
> 
> File.open(APP\_CONFIG\_FILE, ‘w’){|f| f.write string}
> 
> When given a block, File.open closes the file after calling the block, which  
> should ensure that everything you wrote to file should have actually been  
> written.

[ADV]: see also  
[http://blog.rubybestpractices.com/posts/rklemme/001-Using\_blocks\_for\_Robustness.html](http://blog.rubybestpractices.com/posts/rklemme/001-Using_blocks_for_Robustness.html)

Cheers

robert

---

<div class="post-metadata">

**Author:** ![emuen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/96bed5/32.png) [@emuen](https://www.ruby-forum.com/u/emuen)\
**Post date:** [December 10, 2009, 2:35pm UTC](https://www.ruby-forum.com/t/how-do-i-ensure-that-file-write-finished/180053/4 "2009-12-10T14:35:59Z")

</div>

> ```
> File.new(APP_CONFIG_FILE, "w").write string
> 
> ```

This file is never closed, so if the process is exited without running  
its finalizers for some reason, buffered data could be lost.

-r

---

<div class="post-metadata">

**Author:** ![emuen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/96bed5/32.png) [@emuen](https://www.ruby-forum.com/u/emuen)\
**Post date:** [December 10, 2009, 4:53pm UTC](https://www.ruby-forum.com/t/how-do-i-ensure-that-file-write-finished/180053/5 "2009-12-10T16:53:10Z")

</div>

It seems that that did the job. Thank you very much 🙂
