# Ruby way to update a file in place

**URL:** <https://rubytalk.org/t/ruby-way-to-update-a-file-in-place/38270>\
**Category:** ruby-talk\
**Created:** [11 June 2007 19:42 UTC](https://rubytalk.org/t/ruby-way-to-update-a-file-in-place/38270 "2007-06-11T19:42:03Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Guest2](https://avatars.discourse-cdn.com/v4/letter/g/439d5e/32.png) [@Guest2](https://rubytalk.org/u/Guest2)\
**Post date:** [11 June 2007 19:42 UTC](https://rubytalk.org/t/ruby-way-to-update-a-file-in-place/38270/1 "2007-06-11T19:42:03Z")

</div>

Hi all,

I just wonder what is the Ruby way to update a file in place. Do I need  
to open a temporary/new file for writing and then rename the temporary  
file?

Thanks,

Li

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Daniel\_Berger2](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/daniel_berger2/32/2243_2.png) [@Daniel\_Berger2](https://rubytalk.org/u/Daniel_Berger2)\
**Post date:** [11 June 2007 19:55 UTC](https://rubytalk.org/t/ruby-way-to-update-a-file-in-place/38270/2 "2007-06-11T19:55:11Z")

</div>

That's the usual way. The other way is to use mmap.

Regards,

Dan

> **···**
>
> On Jun 11, 1:42 pm, Li Chen \<chen\_...@yahoo.com\> wrote:
> 
> > Hi all,
> > 
> > I just wonder what is the Ruby way to update a file in place. Do I need  
> > to open a temporary/new file for writing and then rename the temporary  
> > file?

---

<div class="post-metadata">

**Author:** ![Joel\_VanderWerf1](https://avatars.discourse-cdn.com/v4/letter/j/94ad74/32.png) [@Joel\_VanderWerf1](https://rubytalk.org/u/Joel_VanderWerf1)\
**Post date:** [11 June 2007 20:03 UTC](https://rubytalk.org/t/ruby-way-to-update-a-file-in-place/38270/3 "2007-06-11T20:03:54Z")

</div>

Li Chen wrote:

> Hi all,
> 
> I just wonder what is the Ruby way to update a file in place. Do I need  
> to open a temporary/new file for writing and then rename the temporary  
> file?

You can open with the "r+" file mode, and use seek/rewind to move to a position within the file, use #truncate to end the file, and so on.

[~/tmp] cat \>foo  
foo  
bar  
baz  
[~/tmp] irb  
irb(main):001:0\> f = File.open("foo", "r+")  
=\> #\<File:foo\>  
irb(main):002:0\> f.puts "FOO"  
=\> nil  
irb(main):003:0\> f.seek 8  
=\> 0  
irb(main):004:0\> f.puts "BAZ"  
=\> nil  
irb(main):005:0\> f.rewind  
=\> 0  
irb(main):006:0\> f.read  
=\> "FOO\nbar\nBAZ\n"  
irb(main):007:0\> f.close  
=\> nil  
irb(main):008:0\> [~/tmp] cat foo  
FOO  
bar  
BAZ

> **···**
>
> --  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

---

<div class="post-metadata">

**Author:** ![Forum](https://avatars.discourse-cdn.com/v4/letter/f/43a26b/32.png) [@Forum](https://rubytalk.org/u/Forum)\
**Post date:** [11 June 2007 20:05 UTC](https://rubytalk.org/t/ruby-way-to-update-a-file-in-place/38270/4 "2007-06-11T20:05:44Z")

</div>

> Hi all,
> 
> I just wonder what is the Ruby way to update a file in place. Do I need  
> to open a temporary/new file for writing and then rename the temporary  
> file?
> 
> Thanks,
> 
> Li
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

No not necessarily, that depends how you can access the file, you can  
use Ruby as an inplace filter on a file

ruby -i.bup -ne 'puts $\_.gsub(/foo/,"bar")' somefile

will edit somefile and create a backup copy somefile.bup of the  
original. Now when it comes to using files inside an application  
things are little bit more complicated ☹

HTH  
Robert

> **···**
>
> On 6/11/07, Li Chen \<chen\_li3@yahoo.com\> wrote:
> 
> > 
> 
> --  
> You see things; and you say Why?  
> But I dream things that never were; and I say Why not?  
> -- George Bernard Shaw

---

<div class="post-metadata">

**Author:** ![Luis1](https://avatars.discourse-cdn.com/v4/letter/l/91b2a8/32.png) [@Luis1](https://rubytalk.org/u/Luis1)\
**Post date:** [11 June 2007 20:08 UTC](https://rubytalk.org/t/ruby-way-to-update-a-file-in-place/38270/5 "2007-06-11T20:08:29Z")

</div>

There is also the '-i' argument to ruby. The example is from the man page:

> **···**
>
> On 6/11/07, Li Chen \<chen\_li3@yahoo.com\> wrote:
> 
> > I just wonder what is the Ruby way to update a file in place. Do I need  
> > to open a temporary/new file for writing and then rename the temporary  
> > file?
> 
> # ================================ % echo matz \> /tmp/junk % cat /tmp/junk matz % ruby -p -i.bak -e "$\_.upcase!" /tmp/junk % cat /tmp/junk MATZ % cat /tmp/junk.bak matz
> 
> --  
> Luis Parravicini  
> [http://ktulu.com.ar/blog/](http://ktulu.com.ar/blog/)

---

<div class="post-metadata">

**Author:** ![lrlebron](https://avatars.discourse-cdn.com/v4/letter/l/a8b319/32.png) [@lrlebron](https://rubytalk.org/u/lrlebron)\
**Post date:** [12 June 2007 02:25 UTC](https://rubytalk.org/t/ruby-way-to-update-a-file-in-place/38270/6 "2007-06-12T02:25:04Z")

</div>

I've used the rio library for this before. [http://rio.rubyforge.org/\](http://rio.rubyforge.org/%5C).  
The website has some very good examples on how to use it.

Luis

> **···**
>
> On Jun 11, 2:42 pm, Li Chen \<chen\_...@yahoo.com\> wrote:
> 
> > Hi all,
> > 
> > I just wonder what is the Ruby way to update a file in place. Do I need  
> > to open a temporary/new file for writing and then rename the temporary  
> > file?
> > 
> > Thanks,
> > 
> > Li
> > 
> > --  
> > Posted viahttp://www.ruby-forum.com/.

---

<div class="post-metadata">

**Author:** ![Guest2](https://avatars.discourse-cdn.com/v4/letter/g/439d5e/32.png) [@Guest2](https://rubytalk.org/u/Guest2)\
**Post date:** [11 June 2007 20:55 UTC](https://rubytalk.org/t/ruby-way-to-update-a-file-in-place/38270/7 "2007-06-11T20:55:07Z")

</div>

Where can I find some information about mmap?

thanks,

Li

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Mike\_B](https://avatars.discourse-cdn.com/v4/letter/m/977dab/32.png) [@Mike\_B](https://rubytalk.org/u/Mike_B)\
**Post date:** [12 June 2007 06:15 UTC](https://rubytalk.org/t/ruby-way-to-update-a-file-in-place/38270/8 "2007-06-12T06:15:04Z")

</div>

That's a nice library...I didn't notice any thing about locking the  
file object. I'm assuming that that would be done outside the use of  
the rio object.

I was also wondering if you thought you could use that library to read  
files that have records that look like:

start line 1  
line 2  
line 3  
end line4

and treat the above four lines as a record?

Mike B.

> **···**
>
> On Jun 11, 6:23 pm, "lrleb...@gmail.com" \<lrleb...@gmail.com\> wrote:
> 
> > On Jun 11, 2:42 pm, Li Chen \<chen\_...@yahoo.com\> wrote:
> > 
> > \> Hi all,
> > 
> > \> I just wonder what is the Ruby way to update a file in place. Do I need  
> > \> to open a temporary/new file for writing and then rename the temporary  
> > \> file?
> > 
> > \> Thanks,
> > 
> > \> Li
> > 
> > \> --  
> > \> Posted viahttp://www.ruby-forum.com/.
> > 
> > I've used the rio library for this before.[http://rio.rubyforge.org/\](http://rio.rubyforge.org/%5C).  
> > The website has some very good examples on how to use it.
> > 
> > Luis

---

<div class="post-metadata">

**Author:** ![Bill\_Guindon1](https://avatars.discourse-cdn.com/v4/letter/b/4da419/32.png) [@Bill\_Guindon1](https://rubytalk.org/u/Bill_Guindon1)\
**Post date:** [11 June 2007 21:11 UTC](https://rubytalk.org/t/ruby-way-to-update-a-file-in-place/38270/9 "2007-06-11T21:11:42Z")

</div>

> Where can I find some information about mmap?

Google is your friend...  
[http://www.google.com/search?source=ig&hl=en&q=ruby+mmap&btnG=Google+Search](http://www.google.com/search?source=ig&hl=en&q=ruby+mmap&btnG=Google+Search)

[http://raa.ruby-lang.org/project/mmap/](http://raa.ruby-lang.org/project/mmap/)

> **···**
>
> On 6/11/07, Li Chen \<chen\_li3@yahoo.com\> wrote:
> 
> > thanks,
> > 
> > Li
> > 
> > --  
> > Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
> 
> --  
> Bill Guindon (aka aGorilla)  
> The best answer to most questions is "it depends".

---

<div class="post-metadata">

**Author:** ![lrlebron](https://avatars.discourse-cdn.com/v4/letter/l/a8b319/32.png) [@lrlebron](https://rubytalk.org/u/lrlebron)\
**Post date:** [12 June 2007 17:05 UTC](https://rubytalk.org/t/ruby-way-to-update-a-file-in-place/38270/10 "2007-06-12T17:05:07Z")

</div>

I used it to clean up some code that was converted from vb6 to vb.net  
The converter left a bunch of comments in the code that started with  
"UPGRADE". I also went ahead and change the old "On Error Go To  
Handler", "ErrHandler", and "Resume Next" strings to vb.net's "Try",  
"Catch ex as Exception" and "End Try" strings

This is what the code looks like

require 'rio'  
rio('C://test').all.files('\*.vb') do |f|  
&nbsp;&nbsp;&nbsp;rio(f) \<f.contents.gsub(/'UPGRADE.\*\s/,"").gsub(/On Error GoTo  
ErrHandler/,"Try").gsub(/ErrHandler:/,"Catch ex as Exception\n").gsub(/  
Resume Next/,"End Try")  
end

The second line tells rio to look at all the files ending in .vb in  
the C://test folder  
The third line does a string substitution in the currently open file

BTW, it was very fast.

Luis

> **···**
>
> On Jun 12, 1:10 am, barjunk \<barj...@attglobal.net\> wrote:
> 
> > On Jun 11, 6:23 pm, "lrleb...@gmail.com" \<lrleb...@gmail.com\> wrote:
> > 
> > \> On Jun 11, 2:42 pm, Li Chen \<chen\_...@yahoo.com\> wrote:
> > 
> > \> \> Hi all,
> > 
> > \> \> I just wonder what is the Ruby way to update a file in place. Do I need  
> > \> \> to open a temporary/new file for writing and then rename the temporary  
> > \> \> file?
> > 
> > \> \> Thanks,
> > 
> > \> \> Li
> > 
> > \> \> --  
> > \> \> Posted viahttp://www.ruby-forum.com/.
> > 
> > \> I've used the rio library for this before.[http://rio.rubyforge.org/\](http://rio.rubyforge.org/%5C).  
> > \> The website has some very good examples on how to use it.
> > 
> > \> Luis
> > 
> > That's a nice library...I didn't notice any thing about locking the  
> > file object. I'm assuming that that would be done outside the use of  
> > the rio object.
> > 
> > I was also wondering if you thought you could use that library to read  
> > files that have records that look like:
> > 
> > start line 1  
> > line 2  
> > line 3  
> > end line4
> > 
> > and treat the above four lines as a record?
> > 
> > Mike B.

---

<div class="post-metadata">

**Author:** ![lrlebron](https://avatars.discourse-cdn.com/v4/letter/l/a8b319/32.png) [@lrlebron](https://rubytalk.org/u/lrlebron)\
**Post date:** [12 June 2007 17:10 UTC](https://rubytalk.org/t/ruby-way-to-update-a-file-in-place/38270/11 "2007-06-12T17:10:23Z")

</div>

Here's an example of using rio  
require 'rio'  
rio('C://test').all.files('\*.vb') do |f|  
&nbsp;&nbsp;&nbsp;rio(f) \<f.contents.gsub(/'UPGRADE.\*\s/,"").gsub(/On Error GoTo  
ErrHandler/,"Try").gsub(/ErrHandler:/,"Catch ex as Exception\n").gsub(/  
Resume Next/,"End Try")  
end

I had converted a bunch of vb6 files to vb.net. The VS2005 convertor  
added hundreds of comment lines that started with 'UPGRADE. I also  
wanted to change the vb6 Exception handlers  
On Error GoTo ErrHandler  
ErrHandler  
Resume Next

to the vb.net format of  
Try  
Catch ex as Exception  
End Try

This little snippet of code did the trick very quickly.

Luis

> **···**
>
> On Jun 12, 1:10 am, barjunk \<barj...@attglobal.net\> wrote:
> 
> > On Jun 11, 6:23 pm, "lrleb...@gmail.com" \<lrleb...@gmail.com\> wrote:
> > 
> > \> On Jun 11, 2:42 pm, Li Chen \<chen\_...@yahoo.com\> wrote:
> > 
> > \> \> Hi all,
> > 
> > \> \> I just wonder what is the Ruby way to update a file in place. Do I need  
> > \> \> to open a temporary/new file for writing and then rename the temporary  
> > \> \> file?
> > 
> > \> \> Thanks,
> > 
> > \> \> Li
> > 
> > \> \> --  
> > \> \> Posted viahttp://www.ruby-forum.com/.
> > 
> > \> I've used the rio library for this before.[http://rio.rubyforge.org/\](http://rio.rubyforge.org/%5C).  
> > \> The website has some very good examples on how to use it.
> > 
> > \> Luis
> > 
> > That's a nice library...I didn't notice any thing about locking the  
> > file object. I'm assuming that that would be done outside the use of  
> > the rio object.
> > 
> > I was also wondering if you thought you could use that library to read  
> > files that have records that look like:
> > 
> > start line 1  
> > line 2  
> > line 3  
> > end line4
> > 
> > and treat the above four lines as a record?
> > 
> > Mike B.

---

<div class="post-metadata">

**Author:** ![Mike\_B](https://avatars.discourse-cdn.com/v4/letter/m/977dab/32.png) [@Mike\_B](https://rubytalk.org/u/Mike_B)\
**Post date:** [11 June 2007 22:25 UTC](https://rubytalk.org/t/ruby-way-to-update-a-file-in-place/38270/12 "2007-06-11T22:25:03Z")

</div>

Although it may be wrong, I recently used lockfile and temporary files  
to do inline editing. It is probably inefficient, but it seems to  
work for what I am using it for.

Maybe someone can point out how to use the -i feature of ruby from  
within the program instead of a shell command, that would work nice  
as well.

In the application I was working on, there were possibilities for  
multiple access at the same time.

Mike B.

> **···**
>
> On Jun 11, 1:11 pm, "Bill Guindon" \<agori...@gmail.com\> wrote:
> 
> > On 6/11/07, Li Chen \<chen\_...@yahoo.com\> wrote:
> > 
> > \> Where can I find some information about mmap?
> > 
> > Google is your friend...[ruby mmap - Google Search](http://www.google.com/search?source=ig&hl=en&q=ruby+mmap&btnG=Google+%5C.%5C.%5C).
> > 
> > [http://raa.ruby-lang.org/project/mmap/](http://raa.ruby-lang.org/project/mmap/)
> > 
> > \> thanks,
> > 
> > \> Li
> > 
> > \> --  
> > \> Posted viahttp://www.ruby-forum.com/.
> > 
> > --  
> > Bill Guindon (aka aGorilla)  
> > The best answer to most questions is "it depends".
